|
| 1 | +<?xml version="1.0"?> |
| 2 | +<entry name="normalizer" type="method" return="String"> |
| 3 | + <title>normalizer</title> |
| 4 | + <signature> |
| 5 | + <argument name="value" type="String"> |
| 6 | + <desc>The value of the element.</desc> |
| 7 | + </argument> |
| 8 | + </signature> |
| 9 | + <desc>Prepares/transforms the elements value for validation.</desc> |
| 10 | + <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> |
| 15 | + </longdesc> |
| 16 | + <example> |
| 17 | + <desc>Makes "field" required and use a normalizer to trim its value before validating</desc> |
| 18 | + <code><![CDATA[ |
| 19 | +$( "#myform" ).validate( { |
| 20 | + rules: { |
| 21 | + field: { |
| 22 | + required: true, |
| 23 | + normalizer: function( value ) { |
| 24 | + // Trim the value of the `field` element before |
| 25 | + // validating. this trims only the value passed |
| 26 | + // to the attached validators, not the value of |
| 27 | + // the element itself. |
| 28 | + return $.trim( value ); |
| 29 | + } |
| 30 | + } |
| 31 | + } |
| 32 | +} ); |
| 33 | +]]></code> |
| 34 | +<html><![CDATA[ |
| 35 | +<label for="field">Required: </label> |
| 36 | +<input class="left" id="field" name="field"> |
| 37 | +<br/> |
| 38 | +<input type="submit" value="Validate!"> |
| 39 | +]]></html> |
| 40 | + </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> |
| 43 | + <code><![CDATA[ |
| 44 | +$( "#myform" ).validate( { |
| 45 | + rules: { |
| 46 | + url_input: { |
| 47 | + required: true, |
| 48 | + url: true, |
| 49 | + normalizer: function( element ) { |
| 50 | + var url = element.value; |
| 51 | +
|
| 52 | + // Check if it doesn't start with http:// or https:// or ftp:// |
| 53 | + if ( url && url.substr( 0, 7 ) !== "http://" |
| 54 | + && url.substr( 0, 8 ) !== "https://" |
| 55 | + && url.substr( 0, 6 ) !== "ftp://" ) { |
| 56 | + // then prefix with http:// |
| 57 | + url = "http://" + url; |
| 58 | + } |
| 59 | +
|
| 60 | + // Return the new url |
| 61 | + return url; |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | +} ); |
| 66 | +]]></code> |
| 67 | +<html><![CDATA[ |
| 68 | +<label for="url_input">url: </label> |
| 69 | +<input class="left" id="url_input" name="url_input"> |
| 70 | +<br/> |
| 71 | +<input type="submit" value="Validate!"> |
| 72 | +]]></html> |
| 73 | + </example> |
| 74 | + <category slug="methods"/> |
| 75 | +</entry> |
0 commit comments