RegularExpressionValidator allows invalid input

  • Thread starter Thread starter Andy Fish
  • Start date Start date
A

Andy Fish

Hi,

I have a RegularExpressionValidator with the following regexp

^[0-9]{0,3}$

It mostly works, but allows a value consisting only of spaces to pass right
through. Leading and trailing spaces (e.g. " 8" in the input) are correctly
rejected.

TIA for any ideas

Andy
 
Andy said:
Hi,

I have a RegularExpressionValidator with the following regexp

^[0-9]{0,3}$

It mostly works, but allows a value consisting only of spaces to pass
right through. Leading and trailing spaces (e.g. " 8" in the input)
are correctly rejected.

TIA for any ideas

Andy

A value consisting only of spaces is considered "empty", so no validators
are run. With one exception: the RequiredFieldValidator.
So you need to add that.

Hans Kesting
 
Hans Kesting said:
Andy said:
Hi,

I have a RegularExpressionValidator with the following regexp

^[0-9]{0,3}$

It mostly works, but allows a value consisting only of spaces to pass
right through. Leading and trailing spaces (e.g. " 8" in the input)
are correctly rejected.

TIA for any ideas

Andy

A value consisting only of spaces is considered "empty", so no validators
are run. With one exception: the RequiredFieldValidator.
So you need to add that.

hmm, that's rather counterintuitive. I would have thought that the point of
a validator is to allow the developer to specify which values are acceptable
rather than have the validation framework arbitrarily consider some values
that fail the regexp as still valid.

In my case I can't use a RequiredFieldValidator as the field is not
mandatory; a blank value is perfectly acceptable but a value containing only
spaces is not.

I guess I will have to just strip leading/trailing blanks before processing
the field value - what a fiasco! still at least I know it was MS rather than
me that was having a brainstorm.

Thanks

Andy
 
Andy,

While Microsoft's validators have this limitation, "Professional Validation
And More" (http://www.peterblum.com/vam/home.aspx) does not. It is a
replacement to Microsoft's validators that greatly expands what you can do
and avoids all the custom coding and hacks.

For example, every validator that evaluates text has a property called Trim.
When true, it trims spaces before evaluating. When false, it does not. You
would set it to false in the case that you've described.

I put together a list of the many limitations that I've found in Microsoft's
validators here: http://www.peterblum.com/vam/valmain.aspx. Even if you
don't switch to Professional Validation And More", it will help you better
plan your site's design.

--- Peter Blum
www.PeterBlum.com
Email: (e-mail address removed)
Creator of "Professional Validation And More" at
http://www.peterblum.com/vam/home.aspx

Andy Fish said:
Hans Kesting said:
Andy said:
Hi,

I have a RegularExpressionValidator with the following regexp

^[0-9]{0,3}$

It mostly works, but allows a value consisting only of spaces to pass
right through. Leading and trailing spaces (e.g. " 8" in the input)
are correctly rejected.

TIA for any ideas

Andy

A value consisting only of spaces is considered "empty", so no validators
are run. With one exception: the RequiredFieldValidator.
So you need to add that.

hmm, that's rather counterintuitive. I would have thought that the point
of a validator is to allow the developer to specify which values are
acceptable rather than have the validation framework arbitrarily consider
some values that fail the regexp as still valid.

In my case I can't use a RequiredFieldValidator as the field is not
mandatory; a blank value is perfectly acceptable but a value containing
only spaces is not.

I guess I will have to just strip leading/trailing blanks before
processing the field value - what a fiasco! still at least I know it was
MS rather than me that was having a brainstorm.

Thanks

Andy
Hans Kesting
 
Back
Top