I want to do preliminary check if entered string looks like Vehicle Identification Number (VIN). I know what it consists of 17 letters and digits, but letters I, O and Q are not allowed inside VIN, so I use this regular expression:
^[0-9A-Z-[IOQ]]{17}$
Now if I check a string like 1G1FP22PXS2100001 with RegularExpressionValidator it fails, but CustomValidator with this OnServerValidate event handler
Regex r = new Regex("^[0-9A-Z-[IOQ]]{17}$");
args.IsValid = r.IsMatch(TextBox1.Text);
works well.
Experiments show what RegularExpressionValidator doesn’t support Character Class Subtraction, but Regex class does.
Now I am interested why do these two .NET classes use different regex flavors? Is it documented somethere?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Not a direct answer but just an obvious remark:
If for some reason Character class subtraction is not supported, you always can use as a workaround:
^[0-9A-HJ-NPR-Z]{17}$
To document what I put in the comments of this question:
The article How to: Validate Against Patterns for ASP.NET Server Controls, does mention that the javascript client-side regex validator does not know “character class subtraction”
As mentionned in RegularExpressionValidator Class .Net documentation:
Both server-side and client-side validation are performed unless the browser does not support client-side validation or client-side validation is explicitly disabled (by setting the
EnableClientScriptproperty to false).The regular-expression validation implementation is slightly different on the client than on the server. On the client, JScript regular-expression syntax is used.
On the server,System.Text.RegularExpressions..::.Regexsyntax is used.
JScript regular expression syntax is a subset ofSystem.Text.RegularExpressions..::.Regexsyntax.
It is therefore recommended that JScript regular-expression syntax should be used in order to yield the same results on both the client and the server.
Another illustration of that side-effect (different regex flavors between server and client sides) is mentionned in RegularExpressionValidator woes blog entry.
Method 2
The RegularExpressionValidator also supports client-side validation using JavaScript, where the JavaScript Regex engine is used. The difference you see is the difference between the JavaScript and the .NET regex implementation.
You can disable client-side validation and thus force the validator to use the .NET regex engine, at the price of the additional post-back.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0