1

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?

1 Answer 1

5

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}$/
Sign up to request clarification or add additional context in comments.

1 Comment

@MuhammadAdeelZahid Additionaly to your answer. The {1} is redundant and you can get rid of it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.