Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
333 views
in Technique[技术] by (71.8m points)

.net - Does RegularExpressionValidator use other flavor than Regex?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 EnableClientScript property 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..::.Regex syntax is used.
JScript regular expression syntax is a subset of System.Text.RegularExpressions..::.Regex syntax.
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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...