0

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>
James Kingsbery
  • 7,298
  • 2
  • 38
  • 67
  • Like @tom said, what have you tried so far? Have you done research to find your answer before posting? There is more then enough documentation about validation on the web. – snaplemouton Jan 31 '14 at 19:42
  • i've tried loads, like changing the type to number and setting a min and max, and some stuff i found on here and yes snaple, i googled it and looked on here. However the ones i found that didnt work for me i could powerpoint as evidence towards my project. – user3258519 Jan 31 '14 at 19:45
  • Is this about client-side validation, or server-side validation (which technology?), or both? There are old questions on both topics, probably with good answers. – Jukka K. Korpela Jan 31 '14 at 21:11
  • Ive worked it out now and will edit my code to the answer incase anyone else shall want to know. – user3258519 Jan 31 '14 at 22:39

3 Answers3

2

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]*/));
});
Community
  • 1
  • 1
scorkla
  • 300
  • 1
  • 9
  • Also, you should probably change your question tag to javascript. – scorkla Jan 31 '14 at 19:46
  • I tried what you said, but it will give me an error unless i only enter two digits – user3258519 Jan 31 '14 at 19:52
  • 1
    Here's scorkla's solution minus the pattern attribute: [fiddle](http://jsfiddle.net/LvD93/) Recommended. – Frankenscarf Jan 31 '14 at 19:57
  • Frankenscarf's link works well. Alternatively, I edited my pattern so it should work. Not stupid, just need to do a lot more html and javascript tutorials. :) – scorkla Jan 31 '14 at 20:02
  • Both the html and javascript sections in fiddle are the code you'd need, but you'd also need to import the [jquery library](http://jquery.com/) in the page(s) head – Frankenscarf Jan 31 '14 at 20:03
  • Can you post it as a reply on here what would work, you see my tutor didnt teach us Html, he gave us a starting code and said use the internet to develop it until the task is reached, and evidence it on powerpoint, so my stupidity in Html is my teachers fault haha – user3258519 Jan 31 '14 at 20:05
  • This will work, and so will the fiddle posted by Frankenscarf. The input I posted will replace what you have in your .html file. The keyup function will go in a javascript (.js) file. You then link the files by doing something like [this](http://programmers.stackexchange.com/questions/91242/what-is-the-best-way-to-include-javascript-file-using-script-tag). I think that's about all the help I can give. :) – scorkla Jan 31 '14 at 20:18
  • Ive worked it out now and will edit my code to the answer incase anyone else shall want to know. – user3258519 Jan 31 '14 at 22:51
0

If you are using PHP, use strlen() for checking the string lenght and is_numeric() for checking that they are numbers.

0

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.

Frankenscarf
  • 1,189
  • 7
  • 10