You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: _entries/validate.md
+26-26Lines changed: 26 additions & 26 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -19,11 +19,11 @@ This method sets up event handlers for submit, focus, keyup, blur and click to t
19
19
20
20
**options***(Object)* (optional)
21
21
22
-
**debug***(Any)* (default: `false`)
22
+
**debug***(Boolean)* (default: `false`)
23
23
24
24
Enables debug mode. If true, the form is not submitted and certain errors are displayed on the console (will check if a `window.console` property exists). Try to enable when a form is just submitted instead of validation stopping the submit. Example: Prevents the form from submitting and tries to help setting up the validation with warnings about missing methods and other debug messages. $("#myform").validate({ debug: true });
25
25
26
-
**submitHandler***(Any)* (default: `native form submit`)
26
+
**submitHandler***(Function)* (default: `native form submit`)
27
27
28
28
Callback for handling the actual submit when the form is valid. Gets the form and the submit event as the only arguments. Replaces the default submit. The right place to submit a form via Ajax after it is validated. Example: Submits the form via Ajax, using [jQuery Form plugin](http://jquery.malsup.com/form), when valid. $("#myform").validate({ submitHandler: function(form) { $(form).ajaxSubmit(); } }); Example: Use submitHandler to process something and then using the default submit. Note that "form" refers to a DOM element, this way the validation isn't triggered again. $("#myform").validate({ submitHandler: function(form) { // do other things for a valid form form.submit(); } }); The callback gets passed two arguments:
29
29
@@ -35,7 +35,7 @@ This method sets up event handlers for submit, focus, keyup, blur and click to t
35
35
36
36
The submit event instance.
37
37
38
-
**invalidHandler***(Any)*
38
+
**invalidHandler***(Function)*
39
39
40
40
Callback for custom code when an invalid form is submitted. Called with an event object as the first argument, and the validator as the second. Example: Displays a message above the form, indicating how many fields are invalid when the user tries to submit an invalid form. $("#myform").validate({ invalidHandler: function(event, validator) { // 'this' refers to the form var errors = validator.numberOfInvalids(); if (errors) { var message = errors == 1 ? 'You missed 1 field. It has been highlighted' : 'You missed ' + errors + ' fields. They have been highlighted'; $("div.error span").html(message); $("div.error").show(); } else { $("div.error").hide(); } } }); The callback gets passed two arguments:
41
41
@@ -47,31 +47,31 @@ This method sets up event handlers for submit, focus, keyup, blur and click to t
47
47
48
48
The validator instance for the current form.
49
49
50
-
**ignore***(Any)* (default: `":hidden"`)
50
+
**ignore***(Selector)* (default: `":hidden"`)
51
51
52
52
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. Example: Ignores all elements with the class "ignore" when validating. $("#myform").validate({ ignore: ".ignore" });
53
53
54
-
**rules***(Any)* (default: `rules are read from markup (classes, attributes, data)`)
54
+
**rules***(Object)* (default: `rules are read from markup (classes, attributes, data)`)
55
55
56
56
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. Example: Specifies a name element as required and an email element as required (using the shortcut for a single rule) and a valid email address (using another object literal). $("#myform").validate({ rules: { // simple rule, converted to {required:true} name: "required", // compound rule email: { required: true, email: true } } }); Example: Specifies a contact element as required and as email address, the latter depending on a checkbox being checked for contact via email. $("#myform").validate({ rules: { contact: { required: true, email: { depends: function(element) { return $("#contactform_email").is(":checked"); } } } } }); Example: Configure a rule that requires a parameter, along with a `depends` callback. $("#myform").validate({ rules: { // at least 15€ when bonus material is included pay_what_you_want: { required: true min: { // min needs a parameter passed to it param: 15, depends: function(element) { return $("#bonus-material").is(":checked"); } } } } });
57
57
58
-
**messages***(Any)* (default: `the default message for the method used`)
58
+
**messages***(Object)* (default: `the default message for the method used`)
59
59
60
60
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, with the rule's parameters as the first argument and the element as the second, and must return a String to display as the message. Example: Specifies a name element as required and an email element as required and a valid email address. A single message is specified for the name element, and two messages for email. $("#myform").validate({ rules: { name: "required", email: { required: true, email: true } }, messages: { name: "Please specify your name", email: { required: "We need your email address to contact you", email: "Your email address must be in the format of name@domain.com" } } }); Example: Validates the name-field as required and having at least two characters. Provides a callback message using jQuery.validator.format to avoid having to specify the parameter in two places. $("#myform").validate({ rules: { name: { required: true, minlength: 2 } }, messages: { name: { required: "We need your email address to contact you", minlength: jQuery.validator.format("At least {0} characters required!") } } });
61
61
62
-
**groups***(Any)*
62
+
**groups***(Object)*
63
63
64
64
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. Example: Use a table layout for the form, placing error messags in the next cell after the input. $("#myform").validate({ groups: { username: "fname lname" }, errorPlacement: function(error, element) { if (element.attr("name") == "fname" || element.attr("name") == "lname" ) { error.insertAfter("#lastname"); } else { error.insertAfter(element); } } });
65
65
66
-
**normalizer***(Any)*
66
+
**normalizer***(Function)*
67
67
68
68
Prepares/transforms the elements value for validation. See [normalizer docs](/normalizer/) for more details.
69
69
70
-
**onsubmit***(Any)* (default: `true`)
70
+
**onsubmit***(Boolean)* (default: `true`)
71
71
72
72
Validate the form on submit. Set to false to use only other events for validation. Example: Disables onsubmit validation, allowing the user to submit whatever he wants, while still validating on keyup/blur/click events (if not specified otherwise). $("#myform").validate({ onsubmit: false });
73
73
74
-
**onfocusout***(Any)*
74
+
**onfocusout***(Boolean | Function)*
75
75
76
76
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. Set to a Function to decide for yourself when to run validation. A boolean true is not a valid value. Example: Disables focusout validation. $("#myform").validate({ onfocusout: false }); The callback gets passed two arguments:
77
77
@@ -83,7 +83,7 @@ This method sets up event handlers for submit, focus, keyup, blur and click to t
83
83
84
84
The event object for this focusout event.
85
85
86
-
**onkeyup***(Any)*
86
+
**onkeyup***(Boolean | Function)*
87
87
88
88
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. Set to false to disable. Set to a Function to decide for yourself when to run validation. A boolean true is not a valid value. Example: Disables onkeyup validation. $("#myform").validate({ onkeyup: false }); The callback gets passed two arguments:
89
89
@@ -95,7 +95,7 @@ This method sets up event handlers for submit, focus, keyup, blur and click to t
95
95
96
96
The event object for this keyup event.
97
97
98
-
**onclick***(Any)*
98
+
**onclick***(Boolean | Function)*
99
99
100
100
Validate checkboxes, radio buttons, and select elements on click. Set to false to disable. Set to a Function to decide for yourself when to run validation. A boolean true is not a valid value. Example: Disables onclick validation of checkboxes, radio buttons, and select elements. $("#myform").validate({ onclick: false }); The callback gets passed two arguments:
101
101
@@ -107,39 +107,39 @@ This method sets up event handlers for submit, focus, keyup, blur and click to t
107
107
108
108
The event object for this click event.
109
109
110
-
**focusInvalid***(Any)* (default: `true`)
110
+
**focusInvalid***(Boolean)* (default: `true`)
111
111
112
112
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 stealing its focus. If there was no element focused, the first one in the form gets it, unless this option is turned off. Example: Disables focusing of invalid elements. $("#myform").validate({ focusInvalid: false });
113
113
114
-
**focusCleanup***(Any)* (default: `false`)
114
+
**focusCleanup***(Boolean)* (default: `false`)
115
115
116
116
If enabled, removes the errorClass from the invalid elements and hides all error messages whenever the element is focused. Avoid combination with focusInvalid. Example: Enables cleanup when focusing elements, removing the error class and hiding error messages when an element is focused. $("#myform").validate({ focusCleanup: true });
117
117
118
-
**errorClass***(Any)* (default: `"error"`)
118
+
**errorClass***(String)* (default: `"error"`)
119
119
120
120
Use this class to create error labels, to look for existing error labels and to add it to invalid elements. Example: Sets the error class to "invalid". $("#myform").validate({ errorClass: "invalid" });
121
121
122
-
**validClass***(Any)* (default: `"valid"`)
122
+
**validClass***(String)* (default: `"valid"`)
123
123
124
124
This class is added to an element after it was validated and considered valid. Example: Sets the valid class to "success". $("#myform").validate({ validClass: "success" });
125
125
126
-
**errorElement***(Any)* (default: `"label"`)
126
+
**errorElement***(String)* (default: `"label"`)
127
127
128
128
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, regardless of element type). Example: Sets the error element to "em". $("#myform").validate({ errorElement: "em" });
129
129
130
-
**wrapper***(Any)* (default: `window`)
130
+
**wrapper***(String)* (default: `window`)
131
131
132
132
Wrap error labels with the specified element. Useful in combination with errorLabelContainer to create a list of error messages. Example: Wrap each error element with a list item, useful when using an ordered or unordered list as the error container. $("#myform").validate({ wrapper: "li" });
133
133
134
-
**errorLabelContainer***(Any)*
134
+
**errorLabelContainer***(Selector)*
135
135
136
136
Hide and show this container when validating. Example: All error labels are displayed inside an unordered list with the ID "messageBox", as specified by the selector passed as errorContainer option. All error elements are wrapped inside a li element, to create a list of messages. $("#myform").validate({ errorLabelContainer: "#messageBox", wrapper: "li", submitHandler: function() { alert("Submitted!") } });
137
137
138
-
**errorContainer***(Any)*
138
+
**errorContainer***(Selector)*
139
139
140
140
Hide and show this container when validating. Example: Uses an additonal container for error messages. The elements given as the errorContainer are all shown and hidden when errors occur. However, the error labels themselves are added to the element(s) given as errorLabelContainer, here an unordered list. Therefore the error labels are also wrapped into li elements (wrapper option). $("#myform").validate({ errorContainer: "#messageBox1, #messageBox2", errorLabelContainer: "#messageBox1 ul", wrapper: "li", debug:true, submitHandler: function() { alert("Submitted!") } });
141
141
142
-
**showErrors***(Any)*
142
+
**showErrors***(Function)*
143
143
144
144
A custom message display handler. Gets the map of errors as the first argument and an 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 on focusout or keyup. You can trigger (in addition to your own messages) the default behaviour by calling this.defaultShowErrors(). Example: Update the number of invalid elements each time an error is displayed. Delegates to the default implementation for the actual error display. $("#myform").validate({ showErrors: function(errorMap, errorList) { $("#summary").html("Your form contains " + this.numberOfInvalids() + " errors, see details below."); this.defaultShowErrors(); } }); The callback gets passed two arguments:
145
145
@@ -159,7 +159,7 @@ This method sets up event handlers for submit, focus, keyup, blur and click to t
159
159
160
160
The DOMElement for this entry.
161
161
162
-
**errorPlacement***(Any)* (default: `Places the error label after the invalid element`)
162
+
**errorPlacement***(Function)* (default: `Places the error label after the invalid element`)
163
163
164
164
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. Example: Use a table layout for the form, placing error messages in the next cell after the input. $("#myform").validate({ errorPlacement: function(error, element) { error.appendTo( element.parent("td").next("td") ); } }); The callback gets passed two arguments:
165
165
@@ -171,7 +171,7 @@ This method sets up event handlers for submit, focus, keyup, blur and click to t
171
171
172
172
The validated input, for relative positioning.
173
173
174
-
**success***(Any)*
174
+
**success***(String | Function)*
175
175
176
176
If specified, the error label is displayed to show a valid element. If a String is given, it is added as a class to the label. If a Function is given, it is called with the label (as a jQuery object) and the validated input (as a DOM element). The label can be used to add a text like "ok!". Example: Add a class "valid" to valid elements, styled via CSS. $("#myform").validate({ success: "valid", submitHandler: function() { alert("Submitted!") } }); Example: Add a class "valid" to valid elements, styled via CSS, and add the text "Ok!". $("#myform").validate({ success: function(label) { label.addClass("valid").text("Ok!") }, submitHandler: function() { alert("Submitted!") } }); The callback gets passed two arguments:
177
177
@@ -183,7 +183,7 @@ This method sets up event handlers for submit, focus, keyup, blur and click to t
183
183
184
184
The element currently being validated, as a DOMElement.
185
185
186
-
**highlight***(Any)* (default: `Adds errorClass (see the option) to the element`)
186
+
**highlight***(Function)* (default: `Adds errorClass (see the option) to the element`)
187
187
188
188
How to highlight invalid fields. Override to decide which fields and how to highlight. Example: Highlights an invalid element by fading it out and in again. $("#myform").validate({ highlight: function(element, errorClass) { $(element).fadeOut(function() { $(element).fadeIn(); }); } }); Example: Adds the error class to both the invalid element and its label $("#myform").validate({ highlight: function(element, errorClass, validClass) { $(element).addClass(errorClass).removeClass(validClass); $(element.form).find("label[for=" + element.id + "]") .addClass(errorClass); }, unhighlight: function(element, errorClass, validClass) { $(element).removeClass(errorClass).addClass(validClass); $(element.form).find("label[for=" + element.id + "]") .removeClass(errorClass); } }); The callback gets passed three arguments:
189
189
@@ -199,10 +199,10 @@ This method sets up event handlers for submit, focus, keyup, blur and click to t
199
199
200
200
Current value of the `validClass` option.
201
201
202
-
**unhighlight***(Any)* (default: `Removes the errorClass`)
202
+
**unhighlight***(Function)* (default: `Removes the errorClass`)
203
203
204
204
Called to revert changes made by option highlight, same arguments as highlight.
205
205
206
-
**ignoreTitle***(Any)* (default: `false`)
206
+
**ignoreTitle***(Boolean)* (default: `false`)
207
207
208
208
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. Example: Configure the plugin to ignore title attributes on validated elements when looking for messages. $("#myform").validate({ ignoreTitle: true });
0 commit comments