rules

function in module validity

rules(rules);

Configure angular-validity's validation rules

Parameters

ParamTypeDetails
rules
(optional)
objectSpecify 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 as max=4, this parameter would contain the value 4)
  • control: the input element

Default Rules

RuleTypeDetailsPattern
emailRegExThe test passes when the value is an email address^[^\s@]+@[^\s@]+.[^\s@]+$
evalfunctionRequires a validity-eval attribute. The value for this attribute must be a JavaScript expression that returns a Boolean valuen/a
maxfunctionRequires 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 <= maxn/a
minfunctionRequires 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 >= minn/a
numberRegExThe test passes when the value is a number^\d+$
requiredfunctionThe test passes when the value is definedn/a

Returns

  • rules - when a parameter is not passed into the function, it returns the existing rules object
  • validity - 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;
    }
});