Keypress event for current value check

keypress, currentValue, preventDefault()

by prasad raja

HTML

<input id="inputBox" type="text" max="100" />

JavaScript

$("#inputBox").on("keypress", function(e){
    var currentValue = String.fromCharCode(e.which);
    var finalValue = $(this).val() + currentValue;
    if(finalValue > parseInt($(this).attr("max")){
        e.preventDefault();
    }
});