wood burning stoves
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
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Regular Expression Help" Watch "Regular Expression Help" New topic

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.

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);
for (var i = 0; i < match.length; i++) {
[ October 18, 2007: Message edited by: Dom Lassy ]
[ October 18, 2007: Message edited by: Dom Lassy ]
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42965
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.
I agree. Here's the link: http://aspose.com/file-tools
subject: Regular Expression Help
It's not a secret anymore!