469,352 Members | 1,694 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,352 developers. It's quick & easy.

Regex for decimal and money

I can validate for simple numbers and decimals with the pattern [^\\d.], but
how to prevent the user from entering something silly like 1.5.6?
Jan 1 '07 #1
3 23231

"Earl" <br******@newsgroups.nospamwrote in message
news:Ow**************@TK2MSFTNGP04.phx.gbl...
>I can validate for simple numbers and decimals with the pattern [^\\d.],
but how to prevent the user from entering something silly like 1.5.6?
Try a pattern that will allow only one "." in the number...But I would not
use a regexp to parse a number. You should try the TryParse of the numeric
types to make sure you got a valid number.

Also I would not consider "1.234" a valid Decimal since I am German, and you
would have to use 1,234 here ;) And TryParse will acknolage the local
culture

Jan 1 '07 #2
rdrunner napisał(a):
>
"Earl" <br******@newsgroups.nospamwrote in message
news:Ow**************@TK2MSFTNGP04.phx.gbl...
>I can validate for simple numbers and decimals with the pattern
[^\\d.], but how to prevent the user from entering something silly
like 1.5.6?

Try a pattern that will allow only one "." in the number...But I would
not use a regexp to parse a number. You should try the TryParse of the
numeric types to make sure you got a valid number.

Also I would not consider "1.234" a valid Decimal since I am German, and
you would have to use 1,234 here ;) And TryParse will acknolage the
local culture

here is a regex:

"(\+|-)?[0-9]+(\.[0-9]*)?"

if you need to check what is the valid decimal separator use

string s =
@"\+|-)?[0-9][0-9]*(\" +
System.Globalization.CultureInfo.CurrentCulture.Nu mberFormat.CurrencyDecimalSeparator
+ @"[0-9]*)?"

best,
Sławomir

Jan 3 '07 #3
Thanks to both of you. I'll consider both ideas.

"Slawek" <sp****************@o2.plwrote in message
news:en**********@node4.news.atman.pl...
rdrunner napisal(a):
>>
"Earl" <br******@newsgroups.nospamwrote in message
news:Ow**************@TK2MSFTNGP04.phx.gbl...
>>I can validate for simple numbers and decimals with the pattern [^\\d.],
but how to prevent the user from entering something silly like 1.5.6?

Try a pattern that will allow only one "." in the number...But I would
not use a regexp to parse a number. You should try the TryParse of the
numeric types to make sure you got a valid number.

Also I would not consider "1.234" a valid Decimal since I am German, and
you would have to use 1,234 here ;) And TryParse will acknolage the local
culture


here is a regex:

"(\+|-)?[0-9]+(\.[0-9]*)?"

if you need to check what is the valid decimal separator use

string s =
@"\+|-)?[0-9][0-9]*(\" +
System.Globalization.CultureInfo.CurrentCulture.Nu mberFormat.CurrencyDecimalSeparator
+ @"[0-9]*)?"

best,
Slawomir

Jan 3 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

21 posts views Thread by Batista, Facundo | last post: by
4 posts views Thread by Mark | last post: by
11 posts views Thread by ZRexRider | last post: by
7 posts views Thread by Paul K | last post: by
2 posts views Thread by tshad | last post: by
3 posts views Thread by Stephan Diehl | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.