maxlength
works for characters typed (inputted), not for the specific numeric format, what you can use is the max=""
attribute and will have to use step
to add from 0.01 to 0.01 ( or 0.1), like this:
<input class="foo" type="number" min="0" max="15.4" maxlength="4" step="0.01">
Note: maxlength=
is with the value 4
and will limit to four digits, in which case the intention is something like 00.00
But of course typing manually will be possible to enter a much larger number, so you can use the events keyup
and blur
, eg:
var foo = document.querySelector(".foo");
//Cria uma função que será usando no keyup e no blue
var f = maxNumber(15.4);
foo.addEventListener('keyup', f);
foo.addEventListener('blur', f);
function maxNumber(max)
{
var running = false;
return function () {
//Para evitar conflito entre o blur e o keyup
if (running) return;
//Bloqueia multiplas chamadas do blur e keyup
running = true;
//Se o input for maior que 15.4 ele irá fixa o valor maximo no value
if (parseFloat(this.value) > max) {
this.value = 15.4;
}
//Habilita novamente as chamadas do blur e keyup
running = false;
};
}
<input class="foo" type="number" min="0" max="15.4" maxlength="4" step="0.01">