Muhammad Adeel Zahid
Muhammad Adeel Zahid

Reputation: 17794

10 digit Regex Validation with angularJs ng-Pattern

I want one of my input fields to have exactly 10 digits. I have used ng-pattern in following manner

<input type="text" ng-pattern="/[1-9]{1}[0-9]{9}$/" ng-model="user.Identity" name="identity">

The problem is that this expression is allowing the user to enter numeric strings having more than 10 digits whereas, I want to allow exactly 10 digits. Is there a problem with my regex or my understanding of ng-pattern?

Upvotes: 1

Views: 9229

Answers (1)

Alex K.
Alex K.

Reputation: 175936

The ^ anchor indicates start of string/line as $ does for the end, ^expr$ prevents the shifting window matches you see:

/^[1-9]{1}[0-9]{9}$/

Upvotes: 5

Related Questions