Solved

Regular Expression for Currency

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

Is Your Team Achieving Their Full Potential?

74% of employees feel they are not achieving their full potential. With Linux Academy, not only will you strengthen your team's core competencies but also their knowledge of of the newest IT topics.

With new material every week, we'll make sure that you stay ahead of the game.

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

627 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