ASP.NET RegularExpressionValidator - WhiteSpace versus "Empty"

S

Shan Plourde

Hi everyone,
I have been using various regular expressions with the ASP.NET
RegularExpressionValidator for quite some time. In general it works
very well. One of the common regex's that I use follows:

ValidationExpression = "^\d{0,3}(\.\d{0,4})?$"

The purpose of this one is to validate that numeric values input
follow the syntax 999.9999. This works well. But, one thing that I
have never tested previously (which has now been uncovered as an
issue) is the situation of whitespace being input into a TextBox.

The following numbers are "caught" by the RegEx above. I surround all
of my examples with Double Quotes just for illustrative purposes.

"999 " -> The space is caught
" 999" The space is caught

However, any of the following fail:
" " -> The RegEx does not "catch" this input as invalid
" " -> Ditto
" " -> etc.

The .NET reference for RegularExpressionValidator notes that:
Validation succeeds if the input control is empty

Even if I add a kludge to the RegEx, such as:
ValidationExpression = "^\s{0}\d{0,3}(\.\d{0,4})?$"

It does not work. My RegEx syntax may not be correct, but I am
assuming that it is and that it should work.

I was unclear as to the practical use of the definition empty. To me I
would expect whitespace to not be considered empty. The reasoning
behind my thinking is that whitespace is indeed captured by the .NET
presentation framework as input that can then be processed on the
server side.

In my case, whitespace is being processed by
Convert.ToDecimal(<value>), where <value> is all whitespace, thereby
causing a runtime error. Whitespace could or could not be considered
valid input in certain applications, so therefore I feel that the
definition of Empty for the purposes of RegEx validation should not
include whitespace.

Has anyone else had this issue, and if so have you been able to
effectively prevent whitespace from being submitted by using some sort
of RegEx? What does this look like?

Thanks,
Shan Plourde
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top