validate jQuery Plugin
Validates the selected form
Syntax
var options; // Type: jQuery.validator.options var instance; // Type: jQuery.validator instance = $(".selector").validate(options);
function validate() : validator;
Browser Compatibility
Options
Name | Description | |
---|---|---|
debug | Enables debug mode. If true, the form is not submitted and certain errors are displayed on the console (requires Firebug or Firebug lite). Try to enable when a form is just submitted instead of validation stopping the submit. | |
errorClass | Use this class to create error labels, to look for existing error labels and to add it to invalid elements. | |
errorContainer | Hide and show this container when validating. | |
errorElement | Use this element type to create error messages and to look for existing error messages. The default, "label", has the advantage of creating a meaningful link between error message and invalid field using the for attribute (which is always used, no matter the element type). | |
errorLabelContainer | Hide and show this container when validating. | |
errorPlacement | Customize placement of created error labels. First argument: The created error label as a jQuery object. Second argument: The invalid element as a jQuery object. | |
focusCleanup | If enabled, removes the errorClass from the invalid elements and hides all errors messages whenever the element is focused. Avoid combination with focusInvalid. | |
focusInvalid | Focus the last active or first invalid element on submit via validator.focusInvalid(). The last active element is the one that had focus when the form was submitted, avoiding to steal its focus. If there was no element focused, the first one in the form gets it, unless this option is turned off. | |
groups | Specify grouping of error messages. A group consists of an arbitrary group name as the key and a space separated list of element names as the value. Use errorPlacement to control where the group message is placed. | |
highlight | How to highlight invalid fields. Override to decide which fields and how to highlight. | |
ignore | Elements to ignore when validating, simply filtering them out. jQuery's not-method is used, therefore everything that is accepted by not() can be passed as this option. Inputs of type submit and reset are always ignored, so are disabled elements. | |
ignoreTitle | Set to skip reading messages from the title attribute, helps to avoid issues with Google Toolbar; default is false for compability, the message-from-title is likely to be completely removed in a future release | |
invalidHandler | Callback for custom code when an invalid form is submitted. Called with a event object as the first argument, and the validator as the second. | |
messages | Key/value pairs defining custom messages. Key is the name of an element, value the message to display for that element. Instead of a plain message another map with specific messages for each rule can be used. Overrides the title attribute of an element or the default message for the method (in that order). Each message can be a String or a Callback. The callback is called in the scope of the validator and with the rule's parameters as the first and the element as the second arugment, it must return a String to display as the message. | |
onclick | Validate checkboxes and radio buttons on click. | |
onfocusout | Validate elements (except checkboxes/radio buttons) on blur. If nothing is entered, all rules are skipped, except when the field was already marked as invalid. | |
onkeyup | Validate elements on keyup. As long as the field is not marked as invalid, nothing happens. Otherwise, all rules are checked on each key up event. | |
onsubmit | Validate the form on submit. Set to false to use only other events for validation. | |
rules | Key/value pairs defining custom rules. Key is the name of an element (or a group of checkboxes/radio buttons), value is an object consisting of rule/parameter pairs or a plain String. Can be combined with class/attribute/data rules. Each rule can be specified as having a depends-property to apply the rule only in certain conditions. See the second example below for details. | |
showErrors | A custom message display handler. Gets the map of errors as the first argument and and array of errors as the second, called in the context of the validator object. The arguments contain only those elements currently validated, which can be a single element when doing validation onblur/keyup. You can trigger (in addition to your own messages) the default behaviour by calling this.defaultShowErrors(). | |
submitHandler | Callback for handling the actual submit when the form is valid. Gets the form as the only argument. Replaces the default submit. The right place to submit a form via Ajax after it validated. | |
success | If specified, the error label is displayed to show a valid element. If a String is given, its added as a class to the label. If a Function is given, its called with the label (as a jQuery object) as its only argument. That can be used to add a text like "ok!". | |
unhighlight | Called to revert changes made by option highlight, same arguments as highlight. | |
validClass | This class is added to an element after it was validated and considered valid. | |
wrapper | Wrap error labels with the specified element. Useful in combination with errorLabelContainer to create a list of error messages. |
Methods
Name | Description | |
---|---|---|
addClassRules | Add a compound class method - useful to refactor common combinations of rules into a single class. | |
addMethod | 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. | |
addWrapper | ||
attributeRules | ||
check | ||
checkable | ||
checkForm | ||
classRules | ||
clean | ||
customDataMessage | ||
customMessage | ||
dataRules | ||
defaultMessage | ||
defaultShowErrors | ||
depend | ||
element | Validates a single element, returns true if it is valid, false otherwise. | |
elements | ||
elementValue | ||
errors | ||
errorsFor | ||
findByName | ||
findDefined | ||
findLastActive | ||
focusInvalid | ||
form | Validates the form, returns true if it is valid, false otherwise. | |
format | ||
formatAndAdd | ||
getLength | ||
hideErrors | ||
idOrName | ||
init | ||
invalidElements | ||
normalizeRule | ||
normalizeRules | ||
numberOfInvalids | Returns the number of invalid fields. | |
objectLength | ||
optional | ||
prepareElement | ||
prepareForm | ||
previousValue | ||
reset | ||
resetForm | Resets the controlled form. | |
setDefaults | Modify default settings for validation. | |
showErrors | Show the specified messages. | |
showLabel | ||
size | ||
startRequest | ||
staticRules | ||
stopRequest | ||
valid | ||
validationTargetFor | ||
validElements |
See Also