Skip to content

Commit 4bb3238

Browse files
committed
Entries: document the ability to define a global normalizer
1 parent 8fbebbd commit 4bb3238

File tree

1 file changed

+60
-8
lines changed

1 file changed

+60
-8
lines changed

entries/normalizer.xml

Lines changed: 60 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,13 @@
88
</signature>
99
<desc>Prepares/transforms the elements value for validation.</desc>
1010
<longdesc>
11-
Note that this method:
12-
<p>Doesn't change the elements' value, it only changes the value used for validation.</p>
13-
<p>Gets the value passed as argument, and "this" within it references the corresponding DOMElement.</p>
14-
<p>Needs to return a String value, otherwise it will throw a TypeError exception.</p>
11+
<p>Transform the value of an element and the result for validation instead of the initial value. The normalizer can be defined global to all elements or local to only one element. With that said, the local normalizer will only run for the element for which it was defined. The global normalizer will run for all validated elements. This normalizer can be then overrided for each element, as needed, by attaching one to it. This way only the local one will run for that element, and the global one will run for others.</p>
12+
<p>Note that this method:</p>
13+
<ul>
14+
<li><p>Doesn't change the elements' value, it only changes the value used for validation.</p></li>
15+
<li><p>Gets the value passed as argument, and "this" within it references the corresponding <code>DOMElement</code>.</p></li>
16+
<li><p>Needs to return a String value, otherwise it will throw a <code>TypeError</code> exception.</p></li>
17+
</ul>
1518
</longdesc>
1619
<example>
1720
<desc>Makes "field" required and use a normalizer to trim its value before validating</desc>
@@ -38,8 +41,8 @@ $( "#myform" ).validate( {
3841
<input type="submit" value="Validate!">
3942
]]></html>
4043
</example>
41-
<example>
42-
<desc>Makes "url" required and use a normalizer to append 'http://', if not present, to the value of the "url" element before validating</desc>
44+
<example>
45+
<desc>Makes "url" required and use a normalizer to append 'http://', if not present, to the value of the "url" element before validating</desc>
4346
<code><![CDATA[
4447
$( "#myform" ).validate( {
4548
rules: {
@@ -51,8 +54,8 @@ $( "#myform" ).validate( {
5154
5255
// Check if it doesn't start with http:// or https:// or ftp://
5356
if ( url && url.substr( 0, 7 ) !== "http://"
54-
&& url.substr( 0, 8 ) !== "https://"
55-
&& url.substr( 0, 6 ) !== "ftp://" ) {
57+
&& url.substr( 0, 8 ) !== "https://"
58+
&& url.substr( 0, 6 ) !== "ftp://" ) {
5659
// then prefix with http://
5760
url = "http://" + url;
5861
}
@@ -69,6 +72,55 @@ $( "#myform" ).validate( {
6972
<input class="left" id="url_input" name="url_input">
7073
<br/>
7174
<input type="submit" value="Validate!">
75+
]]></html>
76+
</example>
77+
78+
<example>
79+
<desc>Using a global normalizer in conjunction with a local one</desc>
80+
<code><![CDATA[
81+
$( "#myform" ).validate( {
82+
// This global normalizer will trim the value of all elements
83+
// before validatng them.
84+
normalizer: function( value ) {
85+
return $.trim( value );
86+
},
87+
rules: {
88+
username: {
89+
required: true
90+
},
91+
url_input: {
92+
required: true,
93+
url: true,
94+
95+
// We don't need to trim the value of this element, so we overrided
96+
// the global normalizer in order to append 'http://' to the url value
97+
// if doesn't already.
98+
normalizer: function( value ) {
99+
var url = value;
100+
101+
// Check if it doesn't start with http:// or https:// or ftp://
102+
if ( url && url.substr( 0, 7 ) !== "http://"
103+
&& url.substr( 0, 8 ) !== "https://"
104+
&& url.substr( 0, 6 ) !== "ftp://" ) {
105+
// then prefix with http://
106+
url = "http://" + url;
107+
}
108+
109+
// Return the new url
110+
return url;
111+
}
112+
}
113+
}
114+
} );
115+
]]></code>
116+
<html><![CDATA[
117+
<label for="username">Required: </label>
118+
<input class="left" id="username" name="username">
119+
<br/>
120+
<label for="url_input">url: </label>
121+
<input class="left" id="url_input" name="url_input">
122+
<br/>
123+
<input type="submit" value="Validate!">
72124
]]></html>
73125
</example>
74126
<category slug="methods"/>

0 commit comments

Comments
 (0)