2

I am new to Regular Expression concept.

I tried to make currency regular expression in which

  1. Amount should be a formatted number string, using ‘,’ for the thousands separator and ‘.’ for the decimal separator.

  2. Amount should have exactly 2 decimal places

  3. Amount should be a nonzero,positive value

I tried this

test1= /^\d{1,3}?([,]\d{3}|\d)*?\.\d\d$/;
test1.test(1,100.00);

But its not fulfilling my requirements.Suggest me how come i achieve this.

Jamiec
  • 133,658
  • 13
  • 134
  • 193
Roli Agrawal
  • 2,356
  • 3
  • 23
  • 28
  • I think you tested `test1.test("1,100.00");`, didn't you? Try [`/^(?!0+\.0+$)\d{1,3}(?:,\d{3}|\d)*\.\d{2}$/.test("1,100.00")`](https://regex101.com/r/uI8rR1/1) – Wiktor Stribiżew Jun 02 '16 at 14:50
  • 2
    Possible duplicate of [Regex currency validation](http://stackoverflow.com/questions/16242449/regex-currency-validation) – Liam Jun 02 '16 at 14:50

2 Answers2

6

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");
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • this is giving true in case of (1150.25) which should be false in my case. – Roli Agrawal Jun 02 '16 at 15:12
  • Why should it be false if you added `\d` to the alternation? Then, you need to remove that alternative. I updated the answer. – Wiktor Stribiżew Jun 02 '16 at 15:14
  • 1
    Thanks a lot. It really work well. I am still new to Regular Expression This will really help me – Roli Agrawal Jun 02 '16 at 15:21
  • this is good but how I do make the comma as optional? So this number `11232339.90` would be accepted? Thanks – sg552 Jun 18 '21 at 08:53
  • 1
    @sg552 See the code snippet, `/^(?!0+\.0+$)\d{1,3}(?:,\d{3}|\d)*\.\d{2}$/` – Wiktor Stribiżew Jun 18 '21 at 09:03
  • Hi it's me again. I tried to group the decimal to `/^(?!0+\.0+$)\d{1,3}(?:,\d{3}|\d)(*\.\d{2})?$/` so decimal point would be optional and this number can be accepted `341` but it didn't work. Can you help me please? – sg552 Jun 18 '21 at 10:25
  • 1
    @sg552 You need to use `(?:` and `)?` to wrap the optional pattern, see [this regex demo](https://regex101.com/r/uI8rR1/15). – Wiktor Stribiżew Jun 18 '21 at 10:28
  • I need help extending the regex. How can I not accept `0`, `00`, `000`, `0.00` or anything that just plain `0` number from this regex? `^(?!0+\.0+$)\d{1,3}(?:,\d{3}|\d)*(?:\.\d{2})?$` . But `0.01` is accepted – sg552 Apr 07 '22 at 05:32
1

If your minimum value is 1.00:

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

This doesn't allow leading zeros.

Toto
  • 89,455
  • 62
  • 89
  • 125