function isNumberKey(e) { var currentChar = parseInt(String.fromCharCode(e.keyCode), 10); if(!isNaN(currentChar)){ var nextValue = $("#txthour").val() + currentChar; //It's a string concatenation, not an addition if(parseInt(nextValue, 10) <= 12) return true; } return false; }