You have to change your input
to one like this, the attribute type
must be number
, and with the attributes max
and min
you give the range you want:
input{
width: 100%;
}
<input type="number" max="8000" min="0" placeholder="Ingrese un número"/>
Now, if you want to validate it with jquery
, you could do the following:
$(document).ready(function(){
const min = 0; // Valor mínimo
const max = 8000; // Valor máximo
// Escuchamos el evento keyup de nuestro input
$(document).on('keyup', '#num', function(){
// Obtenemos el objeto
var self = $(this);
// Obtenemos el valor actual
var value = self.val();
// Si el valor obtenido es menor a nuestro valor mínimo
// o nuestro valor valor obtenido es mayor a nuestro valor máximo
// Le decimos al usuario que no está dentro del rango
// y limpiamos nuestro campo
if(value < min || value > max){
console.log('El número ingresado no está dentro del rango permitido');
self.val('');
}
})
});
input{
width: 100%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="num" type="number" max="8000" min="0" placeholder="Ingrese un número"/>