JSFiddle - React, Tailwind, and code Playground
JavaScript
// Instruções iniciais
function mascara(o,f){
v_obj=o
v_fun=f
setTimeout("execmascara()",1)
}
function execmascara(){
v_obj.value=v_fun(v_obj.value)
}
function remove(str, sub) {
i = str.indexOf(sub);
r = "";
if (i == -1) return str;{
r += str.substring(0,i) + remove(str.substring(i + sub.length), sub);
}
return r;
}
// Formatação especial > OnKeyPress="formatareasy('##/##/####', this)" */
function formatareasy(mascaraformeasy, documentodata){
var i = documentodata.value.length;
var saida = mascaraformeasy.substring(0,1);
var texto = mascaraformeasy.substring(i);
if (texto.substring(0,1) != saida){
documentodata.value += texto.substring(0,1);
}
}
// Máscara telefone
function mtel(v){
v=v.replace(/\D/g,"");
v=v.replace(/^(\d{2})(\d)/g,"($1) $2");
v=v.replace(/(\d)(\d{4})$/,"$1-$2");
return v;
}
// Máscara CEP
function cepm(v){
v=v.replace(/\D/g,"")
v=v.replace(/^(\d{5})(\d)/g,"$1-$2")
return v
}
// Máscsra CPF
function cpf_mask(v){
v=v.replace(/\D/g,"")
v=v.replace(/(\d{3})(\d)/,"$1.$2")
v=v.replace(/(\d{3})(\d)/,"$1.$2")
v=v.replace(/(\d{3})(\d)/,"$1-$2")
return v
}
// Máscara somente número
function soNumeros(v){
return v.replace(/\D/g,"")
}
// Validação CPF
function validarCPF(cpf) {
var filtro = /^\d{3}.\d{3}.\d{3}-\d{2}$/i;
if (!filtro.test(cpf)) {
alert('Por favor, digite um CPF verdadeiro e v\u00e1lido!');
v_obj.value = '';
return false;
}
cpf = remove(cpf, ".");
cpf = remove(cpf, "-");
if (cpf.length != 11 || cpf == "00000000000" || cpf == "11111111111" ||
cpf == "22222222222" || cpf == "33333333333" || cpf == "44444444444" ||
cpf == "55555555555" || cpf == "66666666666" || cpf == "77777777777" ||
cpf == "88888888888" || cpf == "99999999999") {
alert('Por favor, digite um CPF verdadeiro e v\u00e1lido!');
v_obj.value = '';
return false;
...