How can I have an input that is limited to 4 numbers/digits, but if text is typed the character length is infinite?
Final working code:
function isNumber (o) {
return ! isNaN (o-0);
}
$("#txt").keyup(function(e){
txtVal = $(this).val();
if(isNumber(txtVal) && txtVal.length>4)
{
$(this).val(txtVal.substring(0,4) )
$(".error").html("4 Numbers Only").show();
return false;
}
});
});
HTML
<input id="txt1" type="text" name="usrname" />
JAVASCRIPT
function isNumber (o) {
return ! isNaN (o-0);
}
$("#txt1").keyup(function(e){
txtVal = $(this).val();
if(isNumber(txtVal) && txtVal.length>4)
{
$(this).val(txtVal.substring(0,4) )
}
});