Reputation: 2466
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 and ‘.’ for the decimal separator.
Amount should have exactly 2 decimal places
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.
Upvotes: 2
Views: 5832
Reputation: 626738
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");
Upvotes: 6
Reputation: 91385
If your minimum value is 1.00
:
^[1-9]\d{0,2}(?:,\d{3})*\.\d\d$
This doesn't allow leading zeros.
Upvotes: 1