Input validations - Allow only numeric and decimal

by Sushil Mahajan

HTML

<input id="p_first"/>
Allow only numeric or decimal values

JavaScript

$('#p_first').keypress(function(event){
            console.log(event.which);
    var isDotAllowed = true;
    if ($(event.currentTarget).val().indexOf(".") > -1){
        isDotAllowed = false;
    }
    if(event.which != 8 && isNaN(String.fromCharCode(event.which))){
        if (String.fromCharCode(event.which) != ".") {           
            event.preventDefault();
        } else {
            if (isDotAllowed == false) {
                event.preventDefault();                    
            } else {
                isDotAllowed = false;
            }
        }
    }
});