I don't know if anyone has pointed this out yet, but there is an error in the the DataAnnotations to JQuery validation mapper. Under the
jQuery.validator.addMethod("regex", ...
there's the following code:
var match = new RegExp(params).exec(value);
return (match && (match.index == 0) && (match[0].length == value.length));
The problem is that "params" from jQuery has the regex pattern wrapped inside a variable, and the RegExp object requires the pattern itself to be the first argument. This causes the RegExp to search for a single space character " " in the string and report the first one as a match, thereby successfully validating the input if it contains a space, regardless of what the validation pattern actually is.
The fix should be something like:
var match = new RegExp(params.pattern).exec(value);
return (match && (match.index == 0) && (match[0].length == value.length));
Arnstein
0 Points
1 Post
Error in the MicrosoftMvcJQueryValidation.js file
Dec 14, 2010 08:04 AM|LINK
Hi!
I don't know if anyone has pointed this out yet, but there is an error in the the DataAnnotations to JQuery validation mapper. Under the
jQuery.validator.addMethod("regex", ...
there's the following code:
var match = new RegExp(params).exec(value); return (match && (match.index == 0) && (match[0].length == value.length));The problem is that "params" from jQuery has the regex pattern wrapped inside a variable, and the RegExp object requires the pattern itself to be the first argument. This causes the RegExp to search for a single space character " " in the string and report the first one as a match, thereby successfully validating the input if it contains a space, regardless of what the validation pattern actually is.
The fix should be something like:
var match = new RegExp(params.pattern).exec(value); return (match && (match.index == 0) && (match[0].length == value.length));jquery validate RegularExpressionAttribute dataannotation