jQuery – Textbox accept only numbers

Implement a functionality for a textbox to allow only numbers in jQuery:

$('.only-digits').on('keypress', function (e) {
    if ((e.keyCode < 48) || (e.keyCode > 57)) {
        return false;
    }
});

Remains the problem that you can paste text inside the textboxes. To prevent this behavior:

$('.only-digits').bind("cut copy paste",function(e) {
    e.preventDefault();
});

Leave a Comment

Your email address will not be published. Required fields are marked *