Solved

Regular Expression for Currency

Posted on 2013-05-21
3
346 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Major Incident Management Communications

Major incidents and IT service outages cost companies millions. Often the solution to minimizing damage is automated communication. Find out more in our Major Incident Management Communications infographic.

Question has a verified solution.

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

Suggested Solutions

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

734 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