2

I need a regular expression that matches currency values similar to here: Regex for Money

i.e. '1000', '1000,0' and '1000,00'

but I need it to work using javascript:

 var RE = /^-{0,1}\d*\.{0,1}\d+$/; //validates decimal format such as 1000.50
    if (locale == "fr") {
        RE = \d+(?:,\d{1,2})? //this line fails when validated using the javascript below
    }
    return (RE.test(valueToValidate));
Community
  • 1
  • 1
FiveTools
  • 5,970
  • 15
  • 62
  • 84
  • possible duplicate of [Regex for Money](http://stackoverflow.com/questions/1028221/regex-for-money) The regex posted there will be no different for javascript... – Esailija Jan 18 '12 at 15:44
  • It is unclear to me what you want the regex to match and not match. – jfriend00 Jan 18 '12 at 15:46

2 Answers2

3

Literal regex in javascript should be defined with / around the pattern

var RE = /^-{0,1}\d*\.{0,1}\d+$/; //validates decimal format such as 1000.50
if (locale == "fr") {
    RE = /\d+(?:,\d{1,2})?/ //this line fails when validated using the javascript below
}
return (RE.test(valueToValidate));
Sam Greenhalgh
  • 5,952
  • 21
  • 37
0

Just a syntax check: did you forget the forward slashes in your second assignment?

Unai Vivi
  • 3,073
  • 3
  • 30
  • 46