Reputation: 609
I'm using regex for validating numbers in angularjs. But my code shows validation error when the number exceeds 9 digits.Could someone please help me to rectify my code such that any digits of number can be entered.
What I have tried is :
ng-pattern="/^\d{0,9}?$/"
Upvotes: 4
Views: 181
Reputation: 9650
Just change the {0,9}
quantifier with the *
. And remove the ?
-- it's useless:
ng-pattern="/^\d*$/"
For decimal values:
ng-pattern="/^\d*(\.\d+)?$/"
Upvotes: 2
Reputation: 425033
I suspect the problem is understanding the difference between:
{0,9}
[0-9]
{0,9}
means "zero to 9 repetitions of the preceding term", which in this case means "zero to 9 digits".
[0-9]
means "a character in the range 0 to 9 (inclusive)", which is the same as \d
.
Try:
/^\d+$/
Which means "all numeric input" and excludes blank input.
To allow decimal input too, escape the dot and make the decimal part optional:
/^\d+(\.\d+)?$/
Which means "at least one digit, optionally followed by a dot and at least one digit".
Upvotes: 3
Reputation: 1227
Use :
"/^\d*?$/"
\d{0,9}
= match 0 to 9 digits
\d*
= match 0 to N digits
\d = digit : 0 or 1 or 2 or 3 or 4 or 5 or 6 or 7 or 8 or 9
Upvotes: 1
Reputation: 571
Just remove the {0,9}? and use * instead like
/^\d*$/
{0,9} mean is any length between 0 and 9
Upvotes: 3