I am working on a regex for prices in specific formats.
Valid inputs are:
- Natural Numbers
- Decimals(upto two places after decimal point) except 0.0 and 0.00
- All the above criteria without leading zeroes (ex- 05 and 08.45 are invalid but 5 and 8.45 are valid)
Examples of valid test cases
1
11
0.10
0.01
1.1
1.00
11.11
Examples of invalid test cases
0
0.
0.0
0.00
0.000
01
001
1.
1.111
1111.1111
00.1
00.11
01.11
Here's what I have tried but can't get it to work
/^(((?!0)\d+)(.\d{1,2})?)|(0\.(([1-9]\d)|(0[1-9])))$/gm
See Demo #1.
/^[1-9]+[0-9]*(.\d{1,2})?$/gm
See Demo #2.
The solution in Regular Expression for Currency doesn't work for me as it validates 00.11
successfully.
Edit : I guess with some fiddling, I got it right this time. Just changed the [1-9]
to [0-9]
and it worked.
^(?![0.]*$|0+[0-9])\d+(?:\.\d{1,2})?$
See Demo #3.
All thanks to Wiktor Stribiżew for helping this newbie out. Much appreciated sir.