Gaspa79 Gaspa79 - 6 months ago 10
HTML Question

Don't let a user type the period character in an HTML5 input type number

So right now the user is able to insert a decimal value in the number field of the HTML5 input type="number". I want to prevent that, so if a user types "5.1" it will insert "51" in the textbox.

Is there any way to do this? Thanks!

Answer

The definite answer is this:

function preventDot(e) {
            var key = e.charCode ? e.charCode : e.keyCode;

            if (key == 46) {
                e.preventDefault();
            }
        }

And bind it on keypress