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: pages/reference.md
+16-14Lines changed: 16 additions & 14 deletions
Original file line number
Diff line number
Diff line change
@@ -11,10 +11,10 @@ The plugin can never replace serverside validation and doesn't intend to do so.
11
11
12
12
# Markup recommendations
13
13
Each input has a label associated with it: The for-attribute of the label refers to the id-attribute of the input.
14
-
<pre><code>
14
+
```html
15
15
<labelfor="firstname">Firstname</label>
16
16
<inputid="firstname"name="fname">
17
-
</code></pre>
17
+
```
18
18
19
19
The name attribute is '''required''' for input elements, the validation plugin doesn't work without it. Usually name and id attributes should have the same value.
20
20
@@ -27,7 +27,7 @@ A validation rule applies one or more validation methods to an input element. Yo
27
27
## Fields with complex names (brackets, dots)
28
28
If your form consists of fields using names that aren't legal JavaScript identifiers, you have to quote those names when using the rules option:
29
29
30
-
<pre><code>
30
+
```js
31
31
$("#myform").validate({
32
32
rules: {
33
33
// no quoting necessary
@@ -38,14 +38,14 @@ If your form consists of fields using names that aren't legal JavaScript identif
38
38
"user.address.street":"required"
39
39
}
40
40
});
41
-
</code></pre>
41
+
```
42
42
43
43
## Refactoring rules
44
44
Whenever you have multiple fields with the same rules and messages, refactoring those can reduce a lot of duplication. Using addMethod and addClassRules are most effective for that.
45
45
46
46
Let's consider an example where you have ten customer fields, each required and with a minlength of 2. You need custom messages for both rules. To avoid having to specify those rules and messages again and again, we can alias existing methods with different messages and group them into a single class:
With that in place, we can add a class customer to all customer fields and be done with it:
61
61
62
-
<pre><code>
62
+
```html
63
63
<inputname="customer1"class="customer">
64
64
<inputname="customer2"class="customer">
65
65
<inputname="customer3"class="customer">
66
-
</code></pre>
66
+
```
67
67
68
68
You can also reuse existing methods inside other custom methods, to reuse certain implementations. For example, if you're writing a custom method for validating email addresses inside a single field, you could call the existing email method for each email:
69
69
@@ -91,12 +91,14 @@ By default, the first invalid element in a form is focused after submitting a fo
91
91
By default, the form submission is prevented when the form is invalid, and submitted as normal when it is valid. You can also handle the submission manually (option submitHandler).
92
92
93
93
## Skipping validation on submit
94
-
To skip validation while still using a submit-button, add a class="cancel" to that input.
94
+
To skip validation while still using a submit-button, add the attribte "formnovalidate" to that input:
This used to work by adding `class="cancel"` to the input, this is now deprecated.
100
102
101
103
[Demo for the cancel button](http://jqueryvalidation.org/files/demo/errorcontainer-demo.html)
102
104
@@ -111,4 +113,4 @@ While developing and debugging the form, you should set the debug option to true
111
113
Some issues are caused by certain form element's names. A name you should avoid is "submit" (for submit buttons and anything else). Browsers expose form elements as properties of the form element, by their name, in this case hiding native methods like submit(). Just don't use name="submit" and you're good.
112
114
113
115
# Validating multiple forms on one page
114
-
The plugin can handle only one form per call. In case you have multiple forms on a single page which you want to validate, you can avoid having to duplicate the plugin settings by modifying the defaults. Use <ahref="/jQuery.validator.setDefaults/">jQuery.validator.setDefaults</a> to override multiple settings at once.
116
+
The plugin can handle only one form per call. In case you have multiple forms on a single page which you want to validate, you can avoid having to duplicate the plugin settings by modifying the defaults. Use <ahref="/jQuery.validator.setDefaults/">jQuery.validator.setDefaults</a> to override multiple settings at once.
0 commit comments