Documentation

Plugins/Validation/rules

From jQuery JavaScript Library

Jump to: navigation, search

« Back to Plugins/Validation

rules( )

Return the validations rules for the first selected element.
There are several ways to specify validation rules.
  • Validation methods without parameters can be specified as classes on the element (recommended)
  • Validation methods with parameters can be specified as attributes (recommended)
  • Both can be specified as metadata using the metadata plugin
  • Both can be specified using the rules-option of the validate()-method

rules( "add", rules )

Adds the specified rules and returns all rules for the first matched element. Requires that the parent form is validated, that is, $("form").validate() is called first.
The rules can also contain a messages-object, specifying custom messages for existing or added rules.
Arguments:
"add"String
rulesOptions
The rules to add. Accepts the same format as the rules-option of the validate-method.


Examples:
Adds minlength: 2 to an element which is already required.

$("#myinput").rules("add", {
 minlength: 2
});

Adds required and minlength: 2 to an element and specifies custom messages for both.

$("#myinput").rules("add", {
 required: true,
 minlength: 2,
 messages: {
   required: "Required input",
   minlength: jQuery.format("Please, at least {0} characters are necessary")
 }
});

NameType


rules( "remove", [rules] )

Removes the specified rules and returns all rules for the first matched element.
Arguments:
"remove"String
rules (Optional)Options
The space-seperated names of rules to remove and return. If left unspecified, removes and return all rules. Manipulates only rules specified via rules-option or via rules("add").


Examples:
Removes all static rules from an element.

$("#myinput").rules("remove");

Removes min and max rules from an element.

$("#myinput").rules("remove", "min max");

NameType