I would like to know how to not allow the insertion of the letter " and ", which probably represents the Euler , in the input type="number
I would like to know how to not allow the insertion of the letter " and ", which probably represents the Euler , in the input type="number
I found something similar in this answer :
var valorNum = document.getElementById("id_input")
valorNum.addEventListener("keydown", function(e) {
// prevent: "e"
if ([69].includes(e.keyCode)) {
e.preventDefault();
}
})
One point you need to see is that when you reach the maximum value of the field and the user clicks on the top or bottom it changes the number and displays the and 2.18723978932187987e+35
), but this code prevents the letter and special characters entered by the user.
You could try this way, where #input would be the id of your input:
In Jquery:
$(function() {
$("#input").keypress(function(event) {
if (event.which != 8 && event.which != 0 && (event.which < 48 || event.which > 57)) {
$(".alert").html("Enter only digits!").show().fadeOut(2000);
return false;
}
});
});
In pure Javascript:
document.querySelector(".your_class").addEventListener("keypress", function (evt) {
if (evt.which != 8 && evt.which != 0 && evt.which < 48 || evt.which > 57)
{
evt.preventDefault();
}
});
One way to do this would be in your HTML:
<input type="number" onkeydown="return FilterInput(event)" onpaste="handlePaste(event)">
And in your JavaScript:
<script>
function FilterInput(event) {
var keyCode = ('which' in event) ? event.which : event.keyCode;
isNotWanted = (keyCode == 69 || keyCode == 101);
return !isNotWanted;
};
function handlePaste (e) {
var clipboardData, pastedData;
// Get pasted data via clipboard API
clipboardData = e.clipboardData || window.clipboardData;
pastedData = clipboardData.getData('Text').toUpperCase();
if(pastedData.indexOf('E')>-1) {
//alert('found an E');
e.stopPropagation();
e.preventDefault();
}
};
</script>