1

I have an input box in a form and I want user to enter number only and maxlength of the number should be 10.

<input type="text" maxlength="10" ng-model="phoneNumber" class="form-control"/>

When I use the method above, I can restrict the user with 10 digit but user can enter letter.

<input type="number" ng-model="phoneNumber" class="form-control"/>

When I use the second method, user can only enter numbers but I cannot restrict to enter only 10 digits.

Instead of warning the user, is there any other option by which I can combine these two attribute?

2
  • It's best to do any validations in the back-end (the user is always in control of the front-end) Commented Jul 23, 2018 at 12:27
  • As a rule of thumb, do data validation in terms of user experience on the front-end, and data validation in terms of security on the back end. Both layers involve validation, but different types. Commented Jul 23, 2018 at 13:00

1 Answer 1

1

Actually input type=number only handles and not allows user to submit the form if the entered number is not in range.

Please refer:

How can I limit possible inputs in a HTML5 "number" element?

More details:

maxlength ignored for input type="number" in Chrome

HTML5 input number min max not working with required

Sign up to request clarification or add additional context in comments.

3 Comments

OP asked to allow only 10 digit long numbers to be entered. With max="99999" (or ng-maxlength="10") you can type as many as you like (even though it's not validated).
stackoverflow.com/questions/18510845/… looks like this one works for me. Thanks!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.