You can put three numeric type fields side by side, it may not be the best solution but it is still an alternative.
Through the attributes min
and max
you can control how many digits the field can receive: if the maximum is two, then the user can enter any value between 10 and 99.
I see this as something good from the point of view of usability, after all if the field only accepts numbers nothing better than opening the numeric keyboard when the user access the site by mobile.
<div id='nextel'>
<input type='number' min='10' max='99'>
<input type='number' min='1' max='9999999'>
<input type='number' min='10' max='99999'>
</div>
As many as three fields, taking the value is no problem. Just one querySelectorAll('#nextel input')
concatenating the value
of each.
# snippet:
function getNextelNumber(){
let nextel = '';
for(let input of document.querySelectorAll('.nextel input'))
nextel += input.value;
return nextel;
}
document.querySelector('button').addEventListener('click', () => {
alert(getNextelNumber());
});
.nextel input {
border:none;
border-bottom:1px solid #ccc;
outline:none;
width: 7ch
}
.nextel input:invalid {
box-shadow: none;
border-bottom-color: red
}
/* Removendo a aparência padrão do campo 'number'. */
input[type='number'] {
-moz-appearance: textfield;
}
input::-webkit-outer-spin-button,
input::-webkit-inner-spin-button {
-webkit-appearance: none;
}
<div class='nextel'>
<input type='number' min='10' max='99'>
<input type='number' min='1' max='9999999'>
<input type='number' min='10' max='99999'>
</div>
<br><br>
<button>Qual é meu Nextel?</button>
Note: This solution is not cross-browser (yet).
– Renan Gomes
Can change the
max
formaxlength
, I will not accept the answer yet because I hope to do in an input, however it is the solution I am using and therefore deserves +1 :) (and then I will launch a reward)– Guilherme Nascimento
maxlength
only works on type inputtext, email, search, password, tel
orurl
:/ has to be in the gambit of the "maximum allowed value" same.– Renan Gomes
good to know, but it was due to the problem of not being cross-browser. For example it would be better to have maxlength at least, even if min does not work ;)
– Guilherme Nascimento