Disable decimals in a input field[also avoiding +,-,alphabets]

1.Example codes

<input type="number" min="1" step="1" 
    onkeypress="return event.charCode >= 48 && event.charCode <= 57" 
    title="Numbers only" value="1">

2.

var inputBox = document.getElementById("inputBox");
            
                var invalidChars = [
                    "-",
                    "+",
                    "e",
                    "."
                ];

                inputBox.addEventListener("input", function() {
                    this.value = this.value.replace(/[e\+\-\.]/gi, "");
                });
                

                inputBox.addEventListener("keydown", function(e) {
                    if (invalidChars.includes(e.key)) {
                        e.preventDefault();
                    }
                });
 

Leave a comment

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