Skip to content

Entries: Added normalizer documentation #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 25, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions entries/normalizer.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?xml version="1.0"?>
<entry name="normalizer" type="method" return="String">
<title>normalizer</title>
<signature>
<argument name="value" type="String">
<desc>The value of the element.</desc>
</argument>
</signature>
<desc>Prepares/transforms the elements value for validation.</desc>
<longdesc>
Note that this method:
<p>Doesn't change the elements' value, it only changes the value used for validation.</p>
<p>Gets the value passed as argument, and "this" within it references the corresponding DOMElement.</p>
<p>Needs to return a String value, otherwise it will throw a TypeError exception.</p>
</longdesc>
<example>
<desc>Makes "field" required and use a normalizer to trim its value before validating</desc>
<code><![CDATA[
$( "#myform" ).validate( {
rules: {
field: {
required: true,
normalizer: function( value ) {
// Trim the value of the `field` element before
// validating. this trims only the value passed
// to the attached validators, not the value of
// the element itself.
return $.trim( value );
}
}
}
} );
]]></code>
<html><![CDATA[
<label for="field">Required: </label>
<input class="left" id="field" name="field">
<br/>
<input type="submit" value="Validate!">
]]></html>
</example>
<example>
<desc>Makes "url" required and use a normalizer to append 'http://', if not present, to the value of the "url" element before validating</desc>
<code><![CDATA[
$( "#myform" ).validate( {
rules: {
url_input: {
required: true,
url: true,
normalizer: function( element ) {
var url = element.value;

// Check if it doesn't start with http:// or https:// or ftp://
if ( url && url.substr( 0, 7 ) !== "http://"
&& url.substr( 0, 8 ) !== "https://"
&& url.substr( 0, 6 ) !== "ftp://" ) {
// then prefix with http://
url = "http://" + url;
}

// Return the new url
return url;
}
}
}
} );
]]></code>
<html><![CDATA[
<label for="url_input">url: </label>
<input class="left" id="url_input" name="url_input">
<br/>
<input type="submit" value="Validate!">
]]></html>
</example>
<category slug="methods"/>
</entry>