Solved

Regular Expression for Currency

Posted on 2013-05-21
3
343 Views
Last Modified: 2013-05-29
I am trying to come up with a regular expression to validate some dynamic controls.  

I need to be able to validate against certain inputs, such as:

Text box can be empty - this is fine, display no error
Text box can have a whole number e.g. 100 - this is fine, display no error
Text box can have a decimal but must be 2 decimal places maximum (1 is allowed) - display error only if greater than 3 decimal places
Text box should not contain just a decimal point - otherwise display an error
Text box should not contain any other characters than digits and 1 decimal point - otherwise display an error

isValid = Regex.IsMatch(item.ItemAmount.ToString(), "^[0-9]+(.[0-9]{1,2})?$");

Open in new window


The above is what I currently have but it doesn't seem to work, on validation it fails for 100, 100.20 etc.
0
Comment
Question by:angus_young_acdc
  • 2
3 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39184541
Try:

^\d+(?:\.\d{1,2})?$

Open in new window

0
 
LVL 15

Author Comment

by:angus_young_acdc
ID: 39184854
Hi Kaufmed,

That one kind of works, if I just enter a '.' it doesn't produce an error.
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 39184902
That should not be the case. The leading "\d+" requires at least one digit--even if you don't enter anything else. However, thinking about this now I did not cover the possibility of no input. The previous needs to be modified:

^(?:\d+(?:\.\d{1,2})?)?$

Open in new window


e.g.

Screenshot
And I totally overlooked your code. In actuality, we pretty much have the same pattern. The only issue with yours is that you did not escape the dot, and dot matches anything. If you escape the dot, and make the whole pattern (excluding start-of-string and end-of-string) optional, then yours should work as well.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question