I’m using plain vanilla JavaScript and need some help with my regex. Money in the following formats has to be allowed, and in these formats only (with no limit on the number of 0s (tens, hundreds, thousands, etc.) for the dollar amounts allowed):
- $25,000
- $25000
- 25,000
- 25000
- 25000.01
- 25,000.99
- 2000.99
- 50.00
- 50
- 1.95
- 1 .99
- 0.25
- $0.25
- 0.2
- 2.3
- 2000.5
- .75
var regex = /^\$?.?[1-9][0-9,]*(.[0-9]{0,2})?$/;
Currently, it’s not allowing amounts like 0.99 to be entered.
Try this
See it here on Regexr
The only thing that is is not matching is your third last example, it has a space before the dot. Is that valid?
Edit:
My frist solution has the restriction, that it would accept numbers starting with 0, like 001. This solution uses a negative lookahead to avoid this:
See it here on Regexr
Solution without lookahead
See it on Regexr