0
votes

If regex validation is ^\d{0,10}(\.\d{0,2})?$, then maximum two digits are required after decimal which is optional.

But it allows "100." also. since the user has not entered any digit after decimal, how to validate and make decimal digits required if dot is entered

2
Hint: What do you think the 0 in {0,2} means?chepner
Are you aware that the regex also matches the empty string (which might or might not be desirable)?Tim Pietzcker
got it I have to make it 1,2user1788115
It can have empty value toouser1788115

2 Answers

0
votes

If you want to require 2 digits after the decimal if a period is provided then make your regex look like:

/^\d{0,10}(\.\d{2,})?$/

\d{2,} means that a mininum of two numbers is required - followed by any number of digits (which is why the expression is open-ended, e.g. 2, - if you require two and only two digits than provide both a lower- and upper-bound: {2,2}

0
votes

It should work

^\d{0,10}(\.\d\d?)?$

Online demo