mirror of
https://github.com/jquery-validation/jquery-validation.git
synced 2025-12-20 09:11:40 +01:00
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:
20
src/core.js
20
src/core.js
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user