Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- jQuery(function(){
- Validation.add('validate-cpf-cnpj', 'Enter a valid CPF/CNPJ.', function validaCPFCNPJ(str) {
- str = str.replace(/[^\d]+/g,'');
- if (str.length > 11) {
- return validaCNPJ(str);
- }
- return validaCPF(str);
- });
- function validaCPF(cpf) {
- var numeros,
- digitos,
- soma,
- i,
- resultado,
- digitos_iguais;
- digitos_iguais = 1;
- if (cpf.length < 11)
- return false;
- for (i = 0; i < cpf.length - 1; i++)
- if (cpf.charAt(i) != cpf.charAt(i + 1)) {
- digitos_iguais = 0;
- break;
- }
- if (!digitos_iguais) {
- numeros = cpf.substring(0, 9);
- digitos = cpf.substring(9);
- soma = 0;
- for (i = 10; i > 1; i--)
- soma += numeros.charAt(10 - i) * i;
- resultado = soma % 11 < 2
- ? 0
- : 11 - soma % 11;
- if (resultado != digitos.charAt(0))
- return false;
- numeros = cpf.substring(0, 10);
- soma = 0;
- for (i = 11; i > 1; i--)
- soma += numeros.charAt(11 - i) * i;
- resultado = soma % 11 < 2
- ? 0
- : 11 - soma % 11;
- if (resultado != digitos.charAt(1))
- return false;
- return true;
- } else
- return false;
- }
- function validaCNPJ(cnpj) {
- if(cnpj == '') return false;
- if (cnpj.length != 14)
- return false;
- // Elimina CNPJs invalidos conhecidos
- if (cnpj == "00000000000000" ||
- cnpj == "11111111111111" ||
- cnpj == "22222222222222" ||
- cnpj == "33333333333333" ||
- cnpj == "44444444444444" ||
- cnpj == "55555555555555" ||
- cnpj == "66666666666666" ||
- cnpj == "77777777777777" ||
- cnpj == "88888888888888" ||
- cnpj == "99999999999999")
- return false;
- // Valida DVs
- tamanho = cnpj.length - 2
- numeros = cnpj.substring(0,tamanho);
- digitos = cnpj.substring(tamanho);
- soma = 0;
- pos = tamanho - 7;
- for (i = tamanho; i >= 1; i--) {
- soma += numeros.charAt(tamanho - i) * pos--;
- if (pos < 2)
- pos = 9;
- }
- resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
- if (resultado != digitos.charAt(0))
- return false;
- tamanho = tamanho + 1;
- numeros = cnpj.substring(0,tamanho);
- soma = 0;
- pos = tamanho - 7;
- for (i = tamanho; i >= 1; i--) {
- soma += numeros.charAt(tamanho - i) * pos--;
- if (pos < 2)
- pos = 9;
- }
- resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
- if (resultado != digitos.charAt(1))
- return false;
- return true;
- };
- //MASCARA CPF E CNPJ NO MESMO CAMPO
- var options = {onKeyPress: function(cpf, e, f, options){
- var masks = ['000.000.000-009', '00.000.000/0000-00'];
- mask = (cpf.length>14) ? masks[1] : masks[0];
- jQuery('.validate-cpf-cnpj').mask(mask, options);
- }};
- jQuery('.validate-cpf-cnpj').mask('000.000.000-009', options);
- });
Add Comment
Please, Sign In to add comment