The ui-validate directive makes it very easy to create custom validator expressions.
AngularJS comes with several built-in validation mechanism for input fields (ngRequired, ngPattern etc.) but using an arbitrary validation function requires creation of custom formatters and / or parsers. The ui-validate directive makes it easy to use any function(s) defined in scope as a validator function(s).
<input name="email" ng-model="email"
ui-validate="{blacklist : 'notBlackListed($value)' }">
Is e-mail black-listed? {{!!form.email.$error.blacklist}}
<input name="password" required ng-model="password">
<input name="confirm_password"
ui-validate=" '$value==password' "
ui-validate-watch=" 'password' ">
Passwords match? {{!!form.confirm_password.$error.validator}}
...
$scope.notBlackListed = function(value) {
var blacklist = ['bad@domain.com','verybad@domain.com'];
return blacklist.indexOf(value) === -1;
}
Create an expression inside a string. If it evaluates to
true the input is valid, the rule name will be validator by default.
ui-validate=" 'validateFoo($value)' "
Input is valid: {{!!myForm.myInput.$error.validator}}
Or define multiple rules by creating an object where the key is the rule name and the value is the expression string.
ui-validate="{foo:'valFoo($value)', bar:'$value == someVar'}"
Foo rule passes: {{!!myForm.myInput.$error.foo}}
Bar rule passes: {{!!myForm.myInput.$error.bar}}