Skip to content

Commit 61cdef2

Browse files
committed
Reference: Markup cleanup, fix skipping-validation
1 parent 0feaf61 commit 61cdef2

File tree

1 file changed

+16
-14
lines changed

1 file changed

+16
-14
lines changed

pages/reference.md

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ The plugin can never replace serverside validation and doesn't intend to do so.
1111

1212
# Markup recommendations
1313
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
1515
<label for="firstname">Firstname</label>
1616
<input id="firstname" name="fname">
17-
</code></pre>
17+
```
1818

1919
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.
2020

@@ -27,7 +27,7 @@ A validation rule applies one or more validation methods to an input element. Yo
2727
## Fields with complex names (brackets, dots)
2828
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:
2929

30-
<pre><code>
30+
```js
3131
$("#myform").validate({
3232
rules: {
3333
// no quoting necessary
@@ -38,14 +38,14 @@ If your form consists of fields using names that aren't legal JavaScript identif
3838
"user.address.street": "required"
3939
}
4040
});
41-
</code></pre>
41+
```
4242

4343
## Refactoring rules
4444
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.
4545

4646
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:
4747

48-
<pre><code>
48+
```js
4949
// alias required to cRequired with new message
5050
$.validator.addMethod("cRequired", $.validator.methods.required,
5151
"Customer name required");
@@ -55,15 +55,15 @@ Let's consider an example where you have ten customer fields, each required and
5555
$.format("Customer name must have at least {0} characters"));
5656
// combine them both, including the parameter for minlength
5757
$.validator.addClassRules("customer", { cRequired: true, cMinlength: 2 });
58-
</code></pre>
58+
```
5959

6060
With that in place, we can add a class customer to all customer fields and be done with it:
6161

62-
<pre><code>
62+
```html
6363
<input name="customer1" class="customer">
6464
<input name="customer2" class="customer">
6565
<input name="customer3" class="customer">
66-
</code></pre>
66+
```
6767

6868
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:
6969

@@ -91,12 +91,14 @@ By default, the first invalid element in a form is focused after submitting a fo
9191
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).
9292

9393
## 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:
95+
96+
```html
97+
<input type="submit" name="go" value="Submit">
98+
<input type="submit" formnovalidate name="cancel" value="Cancel">
99+
```
95100

96-
<pre><code>
97-
<input type="submit" name="submit" value="Submit">
98-
<input type="submit" class="cancel" name="cancel" value="Cancel">
99-
</code></pre>
101+
This used to work by adding `class="cancel"` to the input, this is now deprecated.
100102

101103
[Demo for the cancel button](http://jqueryvalidation.org/files/demo/errorcontainer-demo.html)
102104

@@ -111,4 +113,4 @@ While developing and debugging the form, you should set the debug option to true
111113
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.
112114

113115
# 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 <a href="/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 <a href="/jQuery.validator.setDefaults/">jQuery.validator.setDefaults</a> to override multiple settings at once.

0 commit comments

Comments
 (0)