Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Test whether string is valid currency 1

Status
Not open for further replies.

Nelviticus

Programmer
Sep 9, 2003
1,819
0
36
GB
I have a string and I need to test whether its contents are convertable to currency - i.e. "36.45" or "123456" would be valid but "36.45.92" or "wubble" would not.

I could [tt]try[/tt] to use [tt]Convert.ToDecimal(sMyString)[/tt] and [tt]catch[/tt] any errors but that seems a bit messy.

I tried to use regular expression matching like this:

Code:
[COLOR=blue]if[/color] (!Regex.IsMatch(sMyString, @"^\\d*\\.?\\d*"))

but I don't really know the regular expression syntax well enough and the above gives false positives and false negatives.

Any idea how to do this?

Nelviticus
 
Can you validate the value on the way in? You really shouldn't allow the value to make it this far if possible. try - catch can take care of any thing that you don't catch with validation. I'm sure given a few mins we could come up with a way to check it.



 
when you specify the @ prior tot he pattern the escaping of the special cahr's is not needed

e.g.
if (!Regex.IsMatch(sMyString, @"^\\d*\\.?\\d*"))
to
if (!Regex.IsMatch(sMyString, @"^\d*\.?\d*"))

try this also
if (!Regex.IsMatch(sMyString, @"^\d*\\.\d*"))



___________________________________________________________________
[sub]
onpnt.com
SELECT * FROM programmers WHERE clue > 0
(0 row(s) affected) -->faq333-3811
[/sub]

 
correction to my copy/paste issues

if (!Regex.IsMatch(sMyString, @"^\d*\.\d*"))

___________________________________________________________________
[sub]
onpnt.com
SELECT * FROM programmers WHERE clue > 0
(0 row(s) affected) -->faq333-3811
[/sub]

 
timothy: the reason that I don't validate on the way in is that there's some validation that can't be done on the web form and I'd rather keep all validation in one place.

onpnt: thanks for the tip on regex strings, although I still can't get it to work. All the variations I try seem to allow through things like '333.444.55', which should be invalid.

In the end I just decided to go with trapping the error - it works and I don't really have time to experiment. That's painful for a perfectionist, but the project's already beyond deadline!

Thanks for the feedback

Nelviticus
 
^[\d]*\.[\d]*$

Now that we can see the invalid value it makes geussing easier [smile]

^ = first position
* = zero or more
\. = escape for char .
[\d] = number
$ = end of string

___________________________________________________________________
[sub]
onpnt.com
SELECT * FROM programmers WHERE clue > 0
(0 row(s) affected) -->faq333-3811
[/sub]

 
Nelviticus,
Keep being a perfectionist man. I'm just like that myself. Here's the correct regular expression string you should use:

@"^\d*\.?\d*$"

You don't have to experiment anything. That's the correct expression to use!

JC

Friends are angels who lift us to our feet when our wings have trouble remembering how to fly...
 
JC:
A small variation to your regex.
yours allowed 0 to n digits after the decimal point.
I believe this will allow only two digits after the decimal point and like yours a decimal point is not required. This regex also requires at least one digit before the decimal point, you can change the + to * if you want to enter .99

Match MyRegMatch = Regex.Match(_input,@"^(\d+)(\.\d{2})?$");

Marty
 
Thanks Marty!

For what Nelviticus needs, however, my Regex is the preferred one. Apart from signs (+/-), my Regex represents any variation of a number:

1 - At most one decimal point.
2 - The decimal point is allowed either at the beginning (with no leading digits) or at the end, which is where the decimal point is by default.
3 - There are no restrictions on the number of digits before or after the decimal point. 0 to infinity are allowed in both directions.

Your Regex is not acceptable because it allows numbers with either 0 or exactly 2 digits only, and the decimal point cannot be at the end. Thus, with your Regex:

* 2.2 fails b/c it doesn't have exactly 2 decimal digits; it only has one.
* 2. fails b/c the decimal point is at the end.
* 2.345 fails b/c it doesn't have exactly 2 decimal digits; it has three.

Now, the above behavior is not desired as 2.2, 2., and 2.234 are all valid numbers and your Regex rejects them.

JC

Friends are angels who lift us to our feet when our wings have trouble remembering how to fly...
 
Thanks people, I don't have time to try these at the moment but will give them a go probably tomorrow.

Nelviticus
 
Thank you JC.
My mistake, I thought it was for currency. Either no decimal point or one decimal with two digits following it.
Marty
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top