Core: Added normalizer

The user can change the value of an element before validating
the element in question. The new value will be then used by
the associated methods instead of the `real one`.

Closes #1602
This commit is contained in:
Brahim Arkni
2015-10-23 20:58:36 +01:00
parent e5e346e599
commit abe6810444
4 changed files with 109 additions and 3 deletions

View File

@@ -627,6 +627,22 @@ $.extend( $.validator, {
val = this.elementValue( element ),
result, method, rule;
// If a normalizer is defined for this element, then
// call it to retreive the changed value instead
// of using the real one.
// Note that `this` in the normalizer is `element`.
if ( typeof rules.normalizer === "function" ) {
val = rules.normalizer.call( element, val );
if ( typeof val !== "string" ) {
throw new TypeError( "The normalizer should return a string value." );
}
// Delete the normalizer from rules to avoid treating
// it as a pre-defined method.
delete rules.normalizer;
}
for ( method in rules ) {
rule = { method: method, parameters: rules[ method ] };
try {
@@ -861,7 +877,7 @@ $.extend( $.validator, {
// meta-characters that should be escaped in order to be used with JQuery
// as a literal part of a name/id or any selector.
escapeCssMeta: function( string ) {
return string.replace( /(!|"|#|\$|%|&|'|\(|\)|\*|\+|,|\.|\/|:|;|<|=|>|\?|@|\[|\\|\]|\^|`|\{|\||\}|~)/g, "\\$1");
return string.replace( /([\\!"#$%&'()*+,./:;<=>?@\[\]^`{|}~])/g, "\\$1" );
},
idOrName: function( element ) {
@@ -1104,7 +1120,7 @@ $.extend( $.validator, {
// evaluate parameters
$.each( rules, function( rule, parameter ) {
rules[ rule ] = $.isFunction( parameter ) ? parameter( element ) : parameter;
rules[ rule ] = $.isFunction( parameter ) && rule !== "normalizer" ? parameter( element ) : parameter;
} );
// clean number parameters