By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,172 Members | 1,092 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,172 IT Pros & Developers. It's quick & easy.

ASP.NET RegularExpressionValidator - WhiteSpace versus "Empty"

P: n/a
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?

Shan Plourde
Nov 18 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.