472,143 Members | 1,187 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,143 software developers and data experts.

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 23439

"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

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.