aspose file tools*
The moose likes HTML, CSS and JavaScript and the fly likes Regular Expression Help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Regular Expression Help" Watch "Regular Expression Help" New topic
Author

Regular Expression Help

Dom Lassy
Ranch Hand

Joined: May 05, 2006
Posts: 181
I would like to come up with some regular expressions to validate numeric form data. I'd like to validate numbers, percentages, and currency values along with validating the range of those numbers.

For example, assume I have the following percentage input:

and the following function:

Can someone help me with coming up with a regular expression to normalize the value? An explanation of the regex would also be helpful because the currency validation is going to be a pain because of the vast array of currencies supported. Thanks in advance.

EDIT:
Here are the requirements that I came up with:
// expression starts with zero or one - character
// the next 1 to n characters must be numbers
// next, the text can contain zero or one instances of (a decimal separator followed by 1 to n numbers)
// expression ends with zero or one % character
// for an input of 12.5% or 12.5 I would like to get match to return 12.5
// if the input is 12.45 I can do the rounding after the normalizing

I figured out the regEx, but I'm having trouble getting match to be 12.5 if 12.5% is entered. Here is the regex and code:
var value = "12.5%";
var regEx = /^(-)?(\d)+(\.(\d)+)*(%)?$/;
var match = regEx.exec(value);
alert(regEx.test(value));
for (var i = 0; i < match.length; i++) {
alert(match[i]);
}
[ October 18, 2007: Message edited by: Dom Lassy ]
[ October 18, 2007: Message edited by: Dom Lassy ]
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39578
    
  27
You're close. The first '+' should be a '*' to account for numbers like '0.5', and the '*' should be a '?' because a number can only have a single decimal point.


Ping & DNS - updated with new look and Ping home screen widget
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Regular Expression Help
 
Similar Threads
Form Validation Help Needed Please
Fiding the error
jQuery custom wrapper method only works half the time
Need help assign java string to java script variable
Regex Issue