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>
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>
<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>
Use maxlength attribute :
<input type = "number" name = "employee_id" size = "20" maxlength= "4">
You can use min and max
<input type="number" name="employee_id" min="1" max="9999">
Referance: How can I limit possible inputs in a HTML5 "number" element?
Easiest way would be to use a jquery plugin, like the one at http://jqueryvalidation.org/.
Since you want to restrict to only 4 digits it'd be something like this:
$( "#myform" ).validate({
rules: {
field: {
required: true,
digits: true,
maxlength: 4
}
}
});