Skip to content Skip to sidebar Skip to footer

Regular Expression For Currency

I am new to Regular Expression concept. I tried to make currency regular expression in which Amount should be a formatted number string, using ‘,’ for the thousands separator

Solution 1:

If you want to disallow 0.00 value, and allow numbers without a digit grouping symbol, you can use

 /^(?!0+\.0+$)\d{1,3}(?:,\d{3})*\.\d{2}$/.test(your_str)

See the regex demo

Explanation:

  • ^ - start of string
  • (?!0+\.0+$) - negative lookahead that fails the match if the input is zero
  • \d{1,3} - 1 to 3 digits
  • (?:,\d{3})* - 0+ sequences of a comma followed with 3 digits
  • \. - a literal dot
  • \d{2} - 2 digits (decimal part)
  • $ - end of string.

document.body.innerHTML = /^(?!0+\.0+$)\d{1,3}(?:,\d{3}|\d)*\.\d{2}$/.test("1,150.25");
document.body.innerHTML += "<br/>" + /^(?!0+\.0+$)\d{1,3}(?:,\d{3}|\d)*\.\d{2}$/.test("0.25");

document.body.innerHTML += "<br/>" + /^(?!0+\.0+$)\d{1,3}(?:,\d{3})*\.\d{2}$/.test("25");
document.body.innerHTML += "<br/>" + /^(?!0+\.0+$)\d{1,3}(?:,\d{3})*\.\d{2}$/.test("0.00");
document.body.innerHTML += "<br/>" + /^(?!0+\.0+$)\d{1,3}(?:,\d{3})*\.\d{2}$/.test("1150.25");

Solution 2:

If your minimum value is 1.00:

^[1-9]\d{0,2}(?:,\d{3})*\.\d\d$

This doesn't allow leading zeros.


Post a Comment for "Regular Expression For Currency"