我想从用户那里检索一个4位数的员工ID。如何将输入字段限制为4位?
<tr>
<td>4 digit Employee ID:</td>
<td><input type = "number" name = "employee_id" size = "20"></td>
</tr>
答案 0 :(得分:3)
<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>
答案 1 :(得分:1)
您可以使用min和max
<input type="number" name="employee_id" min="1" max="9999">
Referance:How can I limit possible inputs in a HTML5 "number" element?
答案 2 :(得分:1)
最简单的方法是使用jquery插件,例如http://jqueryvalidation.org/处的插件。
由于你想限制只有4位数,所以它是这样的:
$( "#myform" ).validate({
rules: {
field: {
required: true,
digits: true,
maxlength: 4
}
}
});
答案 3 :(得分:-1)
使用maxlength属性:
<input type = "number" name = "employee_id" size = "20" maxlength= "4">