Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to restrict user to type 10 digit numbers in input element?

I want to create a input field for phone number. I am creating input field using JavaScript dynamically.

<input type="text" id="phone" name="phone">

How to restrict users to type only 10 digit numbers in order to get a phone number.

like image 870
Vivek Kumar Avatar asked Oct 18 '13 08:10

Vivek Kumar


People also ask

How do I restrict only 10 numbers in a text box in HTML?

Firstly, create an array that stores all the ASCII codes or values of the digits 0 (ASCII value 48) through 9 (ASCII value 57) so that the input in the textbox can be validated later on. Next, set the max length of the input to 10 using maxlength attribute of the input tag.

How do I restrict a number in input field?

Complete HTML/CSS Course 2022 To limit an HTML input box to accept numeric input, use the <input type="number">. With this, you will get a numeric input field. After limiting the input box to number, if a user enters text and press submit button, then the following can be seen “Please enter a number.”


2 Answers

try this

<input type="text" name="country_code" title="Error Message" pattern="[1-9]{1}[0-9]{9}"> 

This will ensure

  1. It is numeric
  2. It will be max of 10 chars
  3. First digit is not zero
like image 145
Vinay Lodha Avatar answered Oct 25 '22 23:10

Vinay Lodha


Use maxlength

<input type="text" maxlength="10" />
like image 21
iJade Avatar answered Oct 26 '22 00:10

iJade