Thursday, June 23, 2005

.NET RegularExpressionValidator control caveat

The RegularExpressionValidator of ASP.NET is a very powerful control for validating user input. You give it a regular expression and, if it's not matched in the input, the message is displayed. But it has a design behavior that may be a little upsetting although documented here: if the user input is empty, the validation passes. No matter if the regular expression requires that at least a character should be introduce. For example the regular expression ^[A-Z][A-Z1-9]*$ requires a first letter. The remaining characters may be letters or numbers. But if the users leaves empty the control to validate, it ignores the required character. The problem may be solved using a second RequiredFieldValidator, but IMO the first validator should check this to conform the regular expression.

0 Comments:

Post a Comment

<< Home