help - regex validator expression not weeding out commas

M

Mad Scientist Jr

i am trying to validate a field for a double, but not allow commas

the regex specifies

any number of whole number digits [0-9]*
no comma [^,]*
an optional decimal point .?
and any number of digits after the decimal [0-9]*

the whole thing looks like this:
[0-9]*[^,]*.?[0-9]*

what am i doing wrong?

as is i am having to use a range validator for double, and give it 0
as the minimum, but i would prefer the regex.

also, can the regex validator be used to "clean" out bad values,
meaning as long as SOME valid characters are in there, it doesn't
generate an error, but when the form is posted, unwanted chars
such as dollar sign and comma are ignored and not posted?

thanks in advance
 
B

bloomfield

First you can try to find a regular expression that suits your needs on
net (for example http://www.regexlib.com/)

Your expression means
any decimal
fallowed by
anything except ','
fallowed by
any character (. means any character if you want the character '.' you
must escape it '/.')
fallowed by
any decimal

So the expression will also match something like 1232asd3218238

For a simple double the following expression should be enough:
^[0-9]*\.?[0-9]*$

this will match any of the fallowing:
23.89
1232
23.
..67

Be aware of the localization problems. I think that using a Compare
validator would be better.

For the additional characters you can let the validator expression be a
little more 'flexible' and remove the unwanted characters on the server.
i am trying to validate a field for a double, but not allow commas

the regex specifies

any number of whole number digits [0-9]*
no comma [^,]*
an optional decimal point .?
and any number of digits after the decimal [0-9]*

the whole thing looks like this:
[0-9]*[^,]*.?[0-9]*

what am i doing wrong?

as is i am having to use a range validator for double, and give it 0
as the minimum, but i would prefer the regex.

also, can the regex validator be used to "clean" out bad values,
meaning as long as SOME valid characters are in there, it doesn't
generate an error, but when the form is posted, unwanted chars
such as dollar sign and comma are ignored and not posted?

thanks in advance
 
M

Mad Scientist Jr

thanks for the reply, and the web site

i have a question, what are the ^ and $ for?
 
B

bloomfield

^ specifies the beginning of the string and $ specifies the end of the
string
See msdn 'Regular Expression Language Elements' for an extensive
description of regular expressions used in .NET

thanks for the reply, and the web site

i have a question, what are the ^ and $ for?

For a simple double the following expression should be enough:
^[0-9]*\.?[0-9]*$
 

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

Top