Last active
November 13, 2024 20:40
-
-
Save ricardodantas/6031749 to your computer and use it in GitHub Desktop.
Máscara e validação de RG, CNPJ, CPF, etc...
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// JavaScript Document | |
// adiciona mascara para rg | |
// Cada estado têm regras e quantidades diferentes de números no registro. Por isso, | |
// não há uma maneira confiável de fazer a validação do mesmo. | |
function MascaraRg(v0,errChar='?'){ | |
const v = v0.toUpperCase().replace(/[^\dX]/g,''); | |
return (v.length==8 || v.length==9)? | |
v.replace(/^(\d{1,2})(\d{3})(\d{3})([\dX])$/,'$1.$2.$3-$4'): | |
(errChar+v0) | |
; | |
} | |
//adiciona mascara de cnpj | |
function MascaraCNPJ(cnpj){ | |
if(mascaraInteiro(cnpj)==false){ | |
event.returnValue = false; | |
} | |
return formataCampo(cnpj, '00.000.000/0000-00', event); | |
} | |
//adiciona mascara de cep | |
function MascaraCep(cep){ | |
if(mascaraInteiro(cep)==false){ | |
event.returnValue = false; | |
} | |
return formataCampo(cep, '00.000-000', event); | |
} | |
//adiciona mascara de data | |
function MascaraData(data){ | |
if(mascaraInteiro(data)==false){ | |
event.returnValue = false; | |
} | |
return formataCampo(data, '00/00/0000', event); | |
} | |
//adiciona mascara ao telefone | |
function MascaraTelefone(tel){ | |
if(mascaraInteiro(tel)==false){ | |
event.returnValue = false; | |
} | |
return formataCampo(tel, '(00) 0000-0000', event); | |
} | |
//adiciona mascara ao CPF | |
function MascaraCPF(cpf){ | |
if(mascaraInteiro(cpf)==false){ | |
event.returnValue = false; | |
} | |
return formataCampo(cpf, '000.000.000-00', event); | |
} | |
//valida telefone | |
function ValidaTelefone(tel){ | |
exp = /\(\d{2}\)\ \d{4}\-\d{4}/ | |
if(!exp.test(tel.value)) | |
alert('Numero de Telefone Invalido!'); | |
} | |
//valida CEP | |
function ValidaCep(cep){ | |
exp = /\d{2}\.\d{3}\-\d{3}/ | |
if(!exp.test(cep.value)) | |
alert('Numero de Cep Invalido!'); | |
} | |
//valida data | |
function ValidaData(data){ | |
exp = /\d{2}\/\d{2}\/\d{4}/ | |
if(!exp.test(data.value)) | |
alert('Data Invalida!'); | |
} | |
//valida o CPF digitado | |
function ValidarCPF(Objcpf){ | |
var cpf = Objcpf.value; | |
exp = /\.|\-/g | |
cpf = cpf.toString().replace( exp, "" ); | |
var digitoDigitado = eval(cpf.charAt(9)+cpf.charAt(10)); | |
var soma1=0, soma2=0; | |
var vlr =11; | |
for(i=0;i<9;i++){ | |
soma1+=eval(cpf.charAt(i)*(vlr-1)); | |
soma2+=eval(cpf.charAt(i)*vlr); | |
vlr--; | |
} | |
soma1 = (((soma1*10)%11)==10 ? 0:((soma1*10)%11)); | |
soma2=(((soma2+(2*soma1))*10)%11); | |
var digitoGerado=(soma1*10)+soma2; | |
if(digitoGerado!=digitoDigitado) | |
alert('CPF Invalido!'); | |
} | |
//valida numero inteiro com mascara | |
function mascaraInteiro(){ | |
if (event.keyCode < 48 || event.keyCode > 57){ | |
event.returnValue = false; | |
return false; | |
} | |
return true; | |
} | |
//valida o CNPJ digitado | |
function ValidarCNPJ(ObjCnpj){ | |
var cnpj = ObjCnpj.value; | |
var valida = new Array(6,5,4,3,2,9,8,7,6,5,4,3,2); | |
var dig1= new Number; | |
var dig2= new Number; | |
exp = /\.|\-|\//g | |
cnpj = cnpj.toString().replace( exp, "" ); | |
var digito = new Number(eval(cnpj.charAt(12)+cnpj.charAt(13))); | |
for(i = 0; i<valida.length; i++){ | |
dig1 += (i>0? (cnpj.charAt(i-1)*valida[i]):0); | |
dig2 += cnpj.charAt(i)*valida[i]; | |
} | |
dig1 = (((dig1%11)<2)? 0:(11-(dig1%11))); | |
dig2 = (((dig2%11)<2)? 0:(11-(dig2%11))); | |
if(((dig1*10)+dig2) != digito) | |
alert('CNPJ Invalido!'); | |
} | |
//formata de forma generica os campos | |
function formataCampo(campo, Mascara, evento) { | |
var boleanoMascara; | |
var Digitato = evento.keyCode; | |
exp = /\-|\.|\/|\(|\)| /g | |
campoSoNumeros = campo.value.toString().replace( exp, "" ); | |
var posicaoCampo = 0; | |
var NovoValorCampo=""; | |
var TamanhoMascara = campoSoNumeros.length;; | |
if (Digitato != 8) { // backspace | |
for(i=0; i<= TamanhoMascara; i++) { | |
boleanoMascara = ((Mascara.charAt(i) == "-") || (Mascara.charAt(i) == ".") | |
|| (Mascara.charAt(i) == "/")) | |
boleanoMascara = boleanoMascara || ((Mascara.charAt(i) == "(") | |
|| (Mascara.charAt(i) == ")") || (Mascara.charAt(i) == " ")) | |
if (boleanoMascara) { | |
NovoValorCampo += Mascara.charAt(i); | |
TamanhoMascara++; | |
}else { | |
NovoValorCampo += campoSoNumeros.charAt(posicaoCampo); | |
posicaoCampo++; | |
} | |
} | |
campo.value = NovoValorCampo; | |
return true; | |
}else { | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Segue uma validador de CPF adaptado e funcional:
function TestaCPF(intCPF) {
let Soma;
let Resto;
Soma = 0;
let CPF = intCPF; CPF = CPF.replace(".",""); CPF = CPF.replace(".",""); CPF = CPF.replace("-","");
const strCPF = CPF;
if (['00000000000', '11111111111', '22222222222','33333333333', '44444444444', '55555555555', '66666666666','77777777777', '88888888888', '99999999999'].includes(strCPF)) return false;
for (i=1; i<=9; i++) Soma = Soma + parseInt(strCPF.substring(i-1, i)) * (11 - i);
Resto = (Soma * 10) % 11;
Soma = 0;
for (i = 1; i <= 10; i++) Soma = Soma + parseInt(strCPF.substring(i-1, i)) * (12 - i);
Resto = (Soma * 10) % 11;
}