• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Regular Expression Help

 
Ranch Hand
Posts: 181
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic