the maxlength
works by typed characters (inputados), and not for the specific numeric format, what you can use is the attribute max=""
and will have to use the step
to add from 0.01 to 0.01 (or 0.1), thus:
<input class="foo" type="number" min="0" max="15.4" maxlength="4" step="0.01">
Note: the maxlength=
this with the value 4
and will limit to four digits, in case the intention is something like 00.00
But of course if you type manually it will be possible to enter a much larger number, then you can use the events keyup
and blur
, example:
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">