Category Archives: Validator

Methods available on the Validator object returned by the validate method.

jQuery.validator.addClassRules()

jQuery.validator.addClassRules( name, rules )

Description: Add a compound class method – useful to refactor common combinations of rules into a single class.

Examples:

Example: Add a new compound rule called “name”, replacing class=”required” minlength=”2″ with class=”name”.

1
2
3
4
jQuery.validator.addClassRules("name", {
required: true,
minlength: 2
});

Example: Add two compound class rules for name and zip.

1
2
3
4
5
6
7
8
9
10
11
12
jQuery.validator.addClassRules({
name: {
required: true,
minlength: 2
},
zip: {
required: true,
digits: true,
minlength: 5,
maxlength: 5
}
});

jQuery.validator.format()

jQuery.validator.format( template, argument, argumentN… )

Description: Replaces {n} placeholders with arguments.

One or more arguments can be passed, in addition to the string template itself, to insert into the string.

If you’re familiar with the term, this makes this function support currying. If you don’t care about that, just use the first argument.

Example:

Sets the debug setting for all validation calls.

1
2
3
var template = jQuery.validator.format("{0} is not a valid value");
// later, results in 'abc is not a valid value'
alert(template("abc"));

jQuery.validator.addMethod()

jQuery.validator.addMethod( name, method [, message ] )

Description: Add a custom validation method. It must consist of a name (must be a legal javascript identifier), a javascript based function and a default string message.

  • jQuery.validator.addMethod( name, method [, message ] )

    • name
      Type: String
      The name of the method used to identify it and referencing it; this must be a valid JavaScript identifier
    • method
      Type: Function()
      The actual method implementation, returning true if an element is valid. First argument: Current value. Second argument: Validated element. Third argument: Parameters.
      • value
        Type: String
        the current value of the validated element
      • element
        Type: Element
        the element to be validated
      • params
        Type: Object
        parameters specified for the method, e.g. for min: 5, the parameter is 5, for range: [1, 5] it’s [1, 5]
    • message
      Type: String
      The default message to display for this method. Can be a function created by ”jQuery.validator.format(value)”. When undefined, an existing message is used (handy for localization), otherwise the field-specific messages have to be defined.
For simple one-off validation, you can use the bundled pattern method (in additional methods, source in src/additional/pattern.js) to validate a field against a regular expression. In general, it is a good idea to encapsulate those regular expressions inside their own method. If you need lots of slightly different expressions, try to extract a common parameter.

See also a library of regular expressions.

Examples:

Example: Add a validation method that checks if a value starts with a certain domain.

1
2
3
jQuery.validator.addMethod("domain", function(value, element) {
return this.optional(element) || /^http:\/\/mycorporatedomain.com/.test(value);
}, "Please specify the correct domain for your documents");

Example: Adds a validation method that checks if a given value equals the addition of the two parameters.

1
2
3
jQuery.validator.addMethod("math", function(value, element, params) {
return this.optional(element) || value == params[0] + params[1];
}, jQuery.validator.format("Please enter the correct value for {0} + {1}"));

Validator.showErrors()

Validator.showErrors( errors )

Description: Show the specified messages.

Example:

Adds and shows error message programmatically.

1
2
3
4
var validator = $( "#myshowErrors" ).validate();
validator.showErrors({
"firstname": "I know that your firstname is Pete, Pete!"
});

Validator.numberOfInvalids()

Validator.numberOfInvalids( errors )

Description: Returns the number of invalid fields.

Example:

Displays a summary of invalid fields after an invalid submit.

1
2
3
4
5
var validator = $( "#myform" ).validate({
invalidHandler: function() {
$( "#summary" ).text( validator.numberOfInvalids() + " field(s) are invalid" );
}
});

Validator.form()

Validator.form()

Description: Validates the form, returns true if it is valid, false otherwise.

Example:

Triggers form validation programmatically.

1
2
var validator = $( "#myform" ).validate();
validator.form();

Validator.element()

Validator.element( element )

Description: Validates a single element, returns true if it is valid, false otherwise.

Example:

Triggers element validation programmatically.

1
2
var validator = $( "#myform" ).validate();
validator.element( "#myselect" );