6

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>
Programmer
  • 1,266
  • 5
  • 23
  • 44

4 Answers4

4
<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>
Programmer
  • 1,266
  • 5
  • 23
  • 44
1

Use maxlength attribute :

<input type = "number" name = "employee_id" size = "20" maxlength= "4">
bumbumpaw
  • 2,522
  • 1
  • 24
  • 54
1

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?

Community
  • 1
  • 1
Afsar
  • 3,104
  • 2
  • 25
  • 35
  • I tried that, but that would allow them to enter non-four digit numbers unfortunately. However, the person below suggested to use the maxlength attribute, I think that will work! Thanks very much for your input though! – Programmer Jan 25 '16 at 04:15
1

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
  }
}

});

Anuar
  • 11
  • 3