I’m pretty sure cannot customize the keyboards on iOS and Android via web pages and if there is something probably is not cross-Platform.
My suggestion to get around the problem is to use type="tel"
(because type="number"
does not allow using different values of numbers inside the value=""
) with a simple ZIP mask, so the keyup
+regex
would already remove what is not a number and what is not a hyphen
I created a very simple example:
document.addEventListener('DOMContentLoaded', function () {
console.log('DOM carregou');
var cepusuario = document.getElementById('cepusuario');
if (!cepusuario) return;
var mascaraCEPTimeout;
cepusuario.addEventListener('input', function () {
if (mascaraCEPTimeout) clearTimeout(mascaraCEPTimeout);
mascaraCEPTimeout = setTimeout(mascaraCEP, 200);
});
function mascaraCEP()
{
var cep = cepusuario.value.replace(/\D+/, ''); //Remove tudo que não for numero
cep = cep.replace(/^(\d{5})(\d{1,3}).*$/, '$1-$2');
cepusuario.value = cep;
}
});
<input type="tel" name="cepusuario" id="cepusuario" class="form-control" placeholder="_____-___" />
Explanations about the regex
A regex /\D+/
remove anything that is not number, then leaving only what is number.
A regex /^(\d{5})(\d{1,3}).*$/
takes the first group (\d{5})
the first 5 numbers and add to replace to $1-
The second group (\d{1,3})
taken from one to three numbers after the fifth number of the value
and applies to $2
that comes after the hyphen.
The .*$
removes any remaining number (or has been typed more), in this case to facilitate it would be interesting to also use maxlength=""
in the <input>
, because it would avoid the person to type the extra, one should use maxlength="9"
, because although the zip code has 8 numbers the hyphen has to be counted as well.
Getting something like:
document.addEventListener('DOMContentLoaded', function () {
console.log('DOM carregou');
var cepusuario = document.getElementById('cepusuario');
if (!cepusuario) return;
var mascaraCEPTimeout;
cepusuario.addEventListener('input', function () {
if (mascaraCEPTimeout) clearTimeout(mascaraCEPTimeout);
mascaraCEPTimeout = setTimeout(mascaraCEP, 200);
});
function mascaraCEP()
{
var cep = cepusuario.value.replace(/\D+/, ''); //Remove tudo que não for numero
cep = cep.replace(/^(\d{5})(\d{1,3}).*$/, '$1-$2');
cepusuario.value = cep;
}
});
<input type="tel" name="cepusuario" id="cepusuario" class="form-control" placeholder="_____-___" maxlength="9" />
I will adapt to use in classes instead of Id’s for my need, it worked perfect, thank you!
– ElvisP
@Eliseub. for nothing, yes it is possible to adapt with classes using a for, or else to simplify could use event delegation, applying keyup (for iOS, event input in delegation does not work on iOS -.-), see how to apply the delegation: https://answall.com/q/210753/3635 ... if you need a "polyfill" to run on older browsers and phones see my answer (which is an addendum to Sérgio’s answer): https://answall.com/a/211420/3635 (delegation helps prevent you from having to add events "manually" and you won’t even need the
DOMContentLoaded
oronload
)– Guilherme Nascimento