Prevent typing non-numeric in input type number

Try preventing the default behaviour if you don’t like the incoming key value:

document.querySelector("input").addEventListener("keypress", function (evt) {
    if (evt.which < 48 || evt.which > 57)
    {
        evt.preventDefault();
    }
});

Leave a Comment