Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I restrict input to 4-digit number

Tags:

html

I want to retrieve a 4-digit employee id from the user. How can I restrict the input field to 4-digit?

<tr>
<td>4 digit Employee ID:</td>
<td><input type = "number" name = "employee_id" size = "20"></td>
</tr>
like image 899
javaprogrammer Avatar asked Jan 25 '16 04:01

javaprogrammer


People also ask

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.”

What will be the code to create a text which accepts only 4 digit number?

Try pattern="\d{4}" . No need to use anchors since the pattern attribute is anchored by default. Maybe type="number" maxlength="4" :-? @ÁlvaroGonzález It doesn't prevent user from entering non-digit characters.

How do I limit a phone number input in HTML?

You can specify a minimum length, in characters, for the entered telephone number using the minlength attribute; similarly, use maxlength to set the maximum length of the entered telephone number.

How do you change the input type to a number?

<input type="number"> <input> elements of type number are used to let the user enter a number.


1 Answers

<tr>
<td>4 digit Employee ID:</td>
<td><input type = "text" name = "employee_id"  pattern="[0-9]{4}" title="4 digit number: e.g. 1234" required></td>
</tr>
like image 52
javaprogrammer Avatar answered Oct 13 '22 13:10

javaprogrammer