How do I restrict "+ - e , ." from HTML number input?
You shouldn't rely only on <input type="number">
, because that would work only in moderns browsers with different behaviours depending on the browser.
Use jQuery to perform additional checks (with a regexp):
$('#my-input').keypress(function() {
var inputValue = $(this).val();
var reg = /^\d+$/;
if (reg.test(inputValue)){
alert("input value is integer");
} else {
alert("input value is not an integer");
}
});
Edit: Boris K has got an even better answer.
Original answer:
This would be a way to accomplish that:
var ageInput = document.getElementById("age")
ageInput.addEventListener("keydown", function(e) {
// prevent: "e", "=", ",", "-", "."
if ([69, 187, 188, 189, 190].includes(e.keyCode)) {
e.preventDefault();
}
})
<input type="number" id="age">