posted 17 years ago
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 ]