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.
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.
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.”
try this
<input type="text" name="country_code" title="Error Message" pattern="[1-9]{1}[0-9]{9}">
This will ensure
Use maxlength
<input type="text" maxlength="10" />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With