For fields where "dates" will be, you could use the attribute type="date"
, but it has no support in Internet Explorer and Safari, according to documentation on MDN.
Only with CSS/HTML you will not be able to create masks in the fields. This type of manipulation is only possible through script. The placeholder
is only indicated to illustrate to the user the format or type of data to be entered.
There are some plugins of mask that you can import, but you can implement with pure Javascript without the need to import anything. See a simple Javascript function that formats the data entered according to the type reported in oninput
:
function mascara(i,t){
var v = i.value;
if(isNaN(v[v.length-1])){
i.value = v.substring(0, v.length-1);
return;
}
if(t == "data"){
i.setAttribute("maxlength", "10");
if (v.length == 2 || v.length == 5) i.value += "/";
}
if(t == "cpf"){
i.setAttribute("maxlength", "14");
if (v.length == 3 || v.length == 7) i.value += ".";
if (v.length == 11) i.value += "-";
}
if(t == "cnpj"){
i.setAttribute("maxlength", "18");
if (v.length == 2 || v.length == 6) i.value += ".";
if (v.length == 10) i.value += "/";
if (v.length == 15) i.value += "-";
}
}
The part below prevents the insertion of another character type other than number:
if(isNaN(v[v.length-1])){
i.value = v.substring(0, v.length-1);
return;
}
At the event oninput
you send to the function the element and the data type to be formatted:
oninput="mascara(this, 'data')"
oninput="mascara(this, 'cpf')"
oninput="mascara(this, 'cnpj')"
And the function itself already delimits the number of characters that will be allowed in the field:
i.setAttribute("maxlength", "14"); // para CPF
See in operation:
function mascara(i,t){
var v = i.value;
if(isNaN(v[v.length-1])){
i.value = v.substring(0, v.length-1);
return;
}
if(t == "data"){
i.setAttribute("maxlength", "10");
if (v.length == 2 || v.length == 5) i.value += "/";
}
if(t == "cpf"){
i.setAttribute("maxlength", "14");
if (v.length == 3 || v.length == 7) i.value += ".";
if (v.length == 11) i.value += "-";
}
if(t == "cnpj"){
i.setAttribute("maxlength", "18");
if (v.length == 2 || v.length == 6) i.value += ".";
if (v.length == 10) i.value += "/";
if (v.length == 15) i.value += "-";
}
if(t == "cep"){
i.setAttribute("maxlength", "9");
if (v.length == 5) i.value += "-";
}
if(t == "tel"){
if(v[0] == 9){
i.setAttribute("maxlength", "10");
if (v.length == 5) i.value += "-";
}else{
i.setAttribute("maxlength", "9");
if (v.length == 4) i.value += "-";
}
}
}
<label for="campo3">Data de Nascimento</label>
<input oninput="mascara(this, 'data')" id="campo3" type="text" class="form-control" placeholder="Ex.: dd/mm/aaaa" autocomplete="off" name="customer['birthdate']">
<br>
<label for="campo4">CPF</label>
<input oninput="mascara(this, 'cpf')" id="campo4" type="text" class="form-control" placeholder="Ex.: xxx.xxx.xxx-xx" autocomplete="off" name="customer['cpf']">
<br>
<label for="campo5">CNPJ</label>
<input oninput="mascara(this, 'cnpj')" id="campo5" type="text" class="form-control" placeholder="Ex.: xx.xxx.xxx/xxxx-xx" autocomplete="off" name="customer['cnpj']">
<br>
<label for="campo6">CEP</label>
<input oninput="mascara(this, 'cep')" id="campo6" type="text" class="form-control" placeholder="Ex.: xxxxx-xxx" autocomplete="off" name="customer['cep']">
<br>
<label for="campo7">Telefone</label>
<input oninput="mascara(this, 'tel')" id="campo7" type="text" class="form-control" placeholder="Ex.: xxxxx-xxxx" autocomplete="off" name="customer['tel']">
you can use
type="date"
and HTML makes a mask and validation– Costamilam
You can find your answer here
– Antonio_Sistema_de_Informacao
@Guilhermecostamilam The problem with type="date" is that it doesn’t have support in IE. It seems to me also that it will want to use other masks without being of dates.
– Sam
Mascara I don’t think CSS does, actually I’m sure. What you can do is use the Type correctly and make Patterns to validate the field with Required. But with the mask, the most you can do is use the Placeholder... At least as far as I know
– hugocsl
In addition to "date", what types of mask you will need?
– Sam
these scripts should be run in header or footer ? or siste js folder ?
– Rodrigo Araújo