<!-- You can put any number you want in maxlength i.e 4 --> <input type="text" maxlength="4" onkeypress='return event.charCode >= 48 && event.charCode <= 57'/>