Solved

Regular Expression for Currency

Posted on 2013-05-21
3
342 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 74

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 74

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video discusses moving either the default database or any database to a new volume.

746 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now