Reputation: 11
I am doing amount validation. The requirements are:
I have tried this regex:
/^(([1-9]\d{0,8})(\.\d{1,2})?)/g;
The issue is, after entering 9 digits, decimal is getting entered only if you enter any digits along with it simultaneously (at a time), without it I am unable enter decimal point.
Upvotes: 1
Views: 75
Reputation: 1047
This could help:
/^(?!0)\d{1,9}\.\d{0,2}$/
In this case, I used a lookahed ((!?0)
) to prevent a leading zero, then used a similar OP expression to match the string. It means that everything in expression \d{1,9}\.\d{0,2}
and not preceded by a zero will be matched.
Upvotes: 1