Only Allow Numeric Value In Input Type Text

Share Now

<input inputmode="numeric" type="text" id="rev_bid" value="18,898" class="form-control">

#NOW THE JS CODE:

$(document).ready(function() {
    $("#rev_bid").keydown(function(event) {
        // Allow only backspace and delete
        if ( event.keyCode == 46 || event.keyCode == 8 ) {
            // let it happen, don't do anything
        }
        else {
            // Ensure that it is a number and stop the keypress
            if (event.keyCode < 48 || event.keyCode > 57 ) {
                event.preventDefault(); 
            }   
        }
    });
});

#JSFiddle:

Or you can just use HTML5 Input Type Number.

Picture of Nahid Mahamud

Nahid Mahamud

Web Developer | Graphic Designer | WordPress & Woo-commerce Expert