Reputation: 1
I am trying to make sure only to validate 4 digits which can only be numbers in a field, and letters cannot be accepted. Its for this field.
<tr>
<td id="Exam_Number">Exam Number</td>
<td><input type="text" name="Exam_Number" /></td>
</tr>
Upvotes: 0
Views: 1222
Reputation: 300
Helpful answer here
Specifically, you'd want to change your input element to:
<input type="text" maxlength="4" pattern="[0-9]{4}" title="Four digit test number" name="Exam_Number"/>
AND use jQuery to only allow digits as shown here
$("#myField").keyup(function() {
$("#myField").val(this.value.match(/[0-9]*/));
});
Upvotes: 2
Reputation: 1219
Setting the input attribute maxlength="4" will restrict the number of characters entered to 4. If your audience is exclusively html5 you can change the input type to "number", otherwise it's going to be a matter of validation and user alerts.
Upvotes: 0
Reputation:
If you are using PHP, use strlen() for checking the string lenght and is_numeric() for checking that they are numbers.
Upvotes: 0