Reputation: 1241
I'm struggling to get a regex working as expected... this is specifically on an ExtJS textfield via regex attribute, but I don't believe it matters, should be a generic JavaScript/Regex question.
Basically, given a string:
1112223334.56
...I want that to be invalid because there's more than 9 digits to the left of the decimal. I've come up with the following regex:
/^(\d{0,9}.{0,1}\d{0,2})$/
That covers almost all the bases: it properly rejects more than one decimal point, or if there's more than two digits to the right of the decimal... and it properly ACCEPTS if there's no decimal portion, or less than 9 digits to the right. So that's all as it should be. The one case it's NOT rejecting that I need it to is more than 9 digits to the left of the decimal.
I've never been a fan of regex so I've been struggling with this for a while, even though I suspect it's a simple answer. Can anyone point out my stupidity? :) Thanks!
Upvotes: 1
Views: 49
Reputation: 624
In regex, the '.' is a special character that actually means 'any character'. So, you need to escape it using a backslash:
^(\d{0,9}\.{0,1}\d{0,2})$
Upvotes: 2