Good afternoon, I'm doing a job, of which I'm going to have several forms with a minimum and maximum number and I'm trying to create a check function and avoid inserting numbers out of range through the keyboard, my function was this way
$(':input[type="number"]').on('keyup', function(event) {
var id = this.id;
var min = document.getElementById(id).getAttribute('min');
var max = document.getElementById(id).getAttribute('max');
verificaTypeNumber(id, min, max);
});
function verificaTypeNumber(idInput, minValue, maxValue) {
var aux = document.getElementById(idInput).value;
if (aux == null || aux < minValue) {
document.getElementById(idInput).value = minValue;
} else if (aux > maxValue) {
document.getElementById(idInput).value = maxValue;
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><inputtype="number" id="Teste" min="10" max="999" value="10">
When you enter a value below the range (in the example less than "10") the function works and enters within if()
, but for a value above the range (above 999), it does not work and does not enter of else if()