Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Regular Expression for Currency

Posted on 2013-05-21
3
Medium Priority
?
349 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 2000 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

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

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 course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

688 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