RegularExpressionValidator.ValidationEx开发者_运维技巧pression="\d{10}"
means only digits - 10 max.
RegularExpressionValidator.ValidationExpression="\d{10,12}"
means only digits - 10, 11 or 12.
How to force strictly 10 or 12 symbols?
One way is:
"\d{10}(\d{2})?"
Or you could be more explicit, at the cost of a little performance:
"^(\d{10}|\d{12})$"
The reason for the anchors in the second expression is described here:
If you experience problems with pattern-matching constructs, try wrapping the expression with "^(" and ")$". For example, "a|ab" becomes "^(a|ab)$".
Update
I was interested in why \d{10}|\d{12}
did not work correctly, and decided to dip into the source code for the validator to see why this fails.
The RegularExpressionValidator
validates both server-side and client-side using the same regular expression and in the case of \d{10}|\d{12}
it fails on the client-side for length 12, but works for length 10. The source-code reveals how the match is made:
var rx = new RegExp(val.validationexpression);
var matches = rx.exec(value);
return (matches != null && value == matches[0]);
Note that this regular expression is A|B
but if A matches, B is never even checked - the regular expression is not "greedy" over the pipe operation - it takes the first match it finds. So the result of matching this regular expression is that the ten digit match succeeds even if you give a 12 digit input. But then the test value == matches[0]
fails because the match is not the full string.
Swapping the order of the terms, i.e. writing \d{12}|\d{10}
, does work because the longer match is tested first, and the shorter match is only tested if the long match fails.
Lesson learned: it is a good idea to explicitly use anchors when using the pipe in RegularExpressionValidator
to avoid having to worry about the order of the terms.
^\d{10}$|^\d{12}$
The two ^$
are important if you want an exact 10 or 12 digits.
By the way, If you are making a lot of regexp this website is great : http://rubular.com/
Have fun
Use the regular expression "pipe"
RegularExpressionValidator.ValidationExpression = "\d{10}|\d{12}"
\d{10}(?:\d{2}){0,1}
should match 10 digits or 10+2 digits. the (?:.....)
part is a non capturing group
精彩评论