two options.
A: In html5 you can set a maxlength. you will then have to check on change what the values entered are and restrict it to 4
B: In html5 you can set the textbox to textmode="number" which will restrict the input to numbers only (and will add a number iterator on the right) You will then have to check the textbox changed to check for the digits length
I don't know why, but these two methods don't work together. Either way you will need to check the input in javascript:
quick and easy:
<input type="text" maxlength="4" onkeyup="this.value = this.value.replace(/[^0-9]/, '')" />
PS: I advise using the jQuery "change" event as that covers copy-paste as well and keystrokes
Hope that helps ^_^
Andy