rules
function in module validity
rules(rules);
rules(rules);
Configure angular-validity's validation rules
Parameters
Param | Type | Details |
---|---|---|
rules (optional) | object | Specify custom validation rules, or override default rules. See Default Rules for a list of rules you can override |
Rule Types
There are two types of validation rules:
RegEx
Requires a regular expression pattern. This pattern is used to test the input's value for a match.
function(value, argument, control)
Requires a function that returns a Boolean value. Three parameters are passed to the function, which relate to the input element being validated:
- value: the value of the input element
- argument: when a validation rule specifies an argument, this is the value of that argument (e.g., the
max
rule requires an argument. When defined asmax=4
, this parameter would contain the value4
) - control: the input element
Default Rules
Rule | Type | Details | Pattern |
---|---|---|---|
RegEx | The test passes when the value is an email address | ^[^\s@]+@[^\s@]+.[^\s@]+$ | |
eval | function | Requires a validity-eval attribute. The value for this attribute must be a JavaScript expression that returns a Boolean value | n/a |
max | function | Requires a numeric parameter. If the value being validated is a string, it will test the length of the string. Otherwise, the value is expected to be numeric. The test passes when the value is <= max | n/a |
min | function | Requires a numeric parameter. If the value being validated is a string, it will test the length of the string. Otherwise, the value is expected to be numeric. The test passes when the value is >= min | n/a |
number | RegEx | The test passes when the value is a number | ^\d+$ |
required | function | The test passes when the value is defined | n/a |
Returns
rules
- when a parameter is not passed into the function, it returns the existing rules objectvalidity
- when a parameter is passed into the function, it returns the validity module (for chaining)
Usage
Call from within your project module's config
method:
angular.module("exampleApp", ["ttValidity"])
.config(["validityProvider", function (validityProvider) {
validityProvider.rules({
// rules
});
}]);
Examples
Example 01: override a default rule
validityProvider.rules({
// The default email rule is a regex. This rule, while quite absurd,
// demonstrates how we can not only override a default rule, but we
// can also switch from a regex to a function while we're at it
email: function (value) {
return value === "[email protected]";
}
});
Example 02: add a new rule (regular expression)
validityProvider.rules({
decimal: /^-?\d+$/
});
Example 03: add a new rule (function with a parameter)
validityProvider.rules({
equals: function (value, expected) {
return value === expected;
}
});
Example 04: add a new rule (deferred)
validityProvider.rules({
// This rule makes an asynchronous call to an API
username: function (value) {
var deferred = $q.defer();
$http.get("/api/username/check", value).then(function (result) {
result.usernameExists && deferred.reject() || deferred.resolve();
}).catch(deferred.reject);
return deferred.promise;
}
});
Updated less than a minute ago