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>
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 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.
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.
<input type="number"> <input> elements of type number are used to let the user enter a number.
<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>
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