Pierre Greborio.NET

Talking about .NET world

RegularExpressionValidator

The RegularExpressionValidator validate an input user control content based on a regular expression. Then, if you want have a string of length between 1 and 20 containing a single word (without blank spaces) you can add the following regular expression to the validator:

^\w{1,20}$

Unfortunately, this isn't sufficient. A note on MSDN library states:

Note   Validation succeeds if the input control is empty. If a value is required for the associated input control, use a RequiredFieldValidator control in addition to the RegularExpressionValidator control.

I sincerly don't agree with that design decision since if the regular expression states that the string must be at least 1 character long, why should I have to use two validators ? The entropy of our code grows uselessly.

The only workaround I found on client side is to edit the function RegularExpressionValidatorEvaluateIsValid on WebUIValidation.js file. Just commenting the following lines of code:

if (ValidatorTrim(value).length == 0)
  return true;

It seems to work now.
Posted: Sep 15 2003, 01:17 PM by PierreG | with 1 comment(s)
Filed under:

Comments

Mike G. said:

What if client-side scripting is disabled for the validator? There is no server-side code to modify. And the handler for a custom validator does not get called when the control is empty...

# August 4, 2004 8:42 AM