Code Monkey home page Code Monkey logo

angular-custom-validation-readme's Introduction

Custom validation with $validators

Overview

The built-in validation in HTML forms can only go so far. However, we can actually define custom validation rules!

Objectives

  • Describe the $validators pipeline
  • Write a custom validator
  • Integrate the custom validator with ngMessages

$validators

Sometimes HTML validators arent enough - for instance, we can't test if data fits to a certain format (like requiring that a string must be 2 characters, 2 numbers and then 2 characters again), or if an input is only numbers.

Let's create a directive to see if our input is only numbers. Up until now we've only used directives as elements with templates. However, we're now creating directives as attributes (remember that we can restrict our directives to only be attributes) and not having a template - by applying our directive to an element, we're affecting the behaviour of that element - not the contents.

Much like $parsers and $formatters, we add our custom validators into $validators. Here we have a function where we can test if the value is valid, returning true or false depending on their validity.

function integer() {
	return {
		restrict: 'A',
		require: 'ngModel',
		link: function (scope, element, attrs, ngModel) {
			ngModel.$validators.integer = function (value) {
				return /^\-?\d+$/.test(value);
			};
		}
	}
}

angular
	.module('app')
	.directive('integer', integer);

You might've noticed we're simply adding to the ngModel.$validators object instead of pushing into it. This is because we need to give our validators a simple name that will then get added to our $errors object if the validation returns false. We can also use these custom validators in ngMessages! To activate this validation on an input, we add it as an attribute like when we use maxlength, etc.

Example usage of our new validator would look like this:

<form name="form">
	<input name="number" ng-model="ctrl.number" integer />

	<div ng-messages="form.number.$errors">
		<div ng-message="integer">Number must be an integer!</div>
	</div>
</form>

Awesome! Custom validation that slots in nicely with the built-in validation we've used previously.

View Angular Custom Validation on Learn.co and start learning to code for free.

angular-custom-validation-readme's People

Contributors

febbraiod avatar gj avatar ipc103 avatar toddmotto avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

angular-custom-validation-readme's Issues

syntax error in validator example, missing package.json

Not sure if this is intentional, but this throws an error:
return value.test(/^-?\d+$/);

The regexp should come first, with (value) following ".test"

Seems like something thrown in to make us pay attention to the console and do some googling, and it's not terribly hard to figure out. But FYI.

Gotten used to copying over package.json from other labs by now, but also FYI: it's missing.

value.test()

In the directive, the line

**return value.test(/^\-?\d+$/);**

should be

**var REGEX = /^\-?\d+$/;**
**return REGEX.test(value);**

possible syntax error

In this lesson, we learn to test the value passed to $validators using return value.test(/^\-?\d+$/); however this syntax failed in the following lab. Instead I had to use return /\d{2}[a-z]{4}\d{2}/i.test(value); to pass the $validators test. I'm wondering if return /^\-?\d+$/.test(value); might be the correct syntax for this lesson?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.