Regex for decimal and money

  • Thread starter Thread starter Earl
  • Start date Start date
E

Earl

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?
 
Earl said:
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
 
rdrunner napisał(a):
Earl said:
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.NumberFormat.CurrencyDecimalSeparator
+ @"[0-9]*)?"

best,
Sławomir
 
Thanks to both of you. I'll consider both ideas.

Slawek said:
rdrunner napisal(a):
Earl said:
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.NumberFormat.CurrencyDecimalSeparator
+ @"[0-9]*)?"

best,
Slawomir
 

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

Similar Threads


Back
Top