Why Is My Javascript Regexp Test Function Not Working?
I've got a Javascript function that tests a regexp that I'm intending to validate positive decimals up to a precision of two: function isPositiveDecimalPrecisionTwo(str) { retu
Solution 1:
The problem is that your $
is inside the parenthesis. You want it to be after the group.
/^\d*(\.\d{1,2})?$/
This should work the way you expect.
With the $
inside, it was matching everything that started with a number, since the end of string ($
) was "optional".
Solution 2:
You can also do it without grouping the decimal portion by just adding a ?
after the escaped decimal point
^\d*\.?\d{1,2}$
Post a Comment for "Why Is My Javascript Regexp Test Function Not Working?"