Advertisement
Guest User

Lavieri

a guest
Apr 2nd, 2010
2,837
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function isCnpj(cnpj) {
  2.     var numeros, digitos, soma, i, resultado, pos, tamanho, digitos_iguais;
  3.     if (cnpj.length == 0) {
  4.         return false;
  5.     }
  6.    
  7.     cnpj = cnpj.replace(/\D+/g, '');
  8.     digitos_iguais = 1;
  9.  
  10.     for (i = 0; i < cnpj.length - 1; i++)
  11.         if (cnpj.charAt(i) != cnpj.charAt(i + 1)) {
  12.             digitos_iguais = 0;
  13.             break;
  14.         }
  15.     if (digitos_iguais)
  16.         return false;
  17.    
  18.     tamanho = cnpj.length - 2;
  19.     numeros = cnpj.substring(0,tamanho);
  20.     digitos = cnpj.substring(tamanho);
  21.     soma = 0;
  22.     pos = tamanho - 7;
  23.     for (i = tamanho; i >= 1; i--) {
  24.         soma += numeros.charAt(tamanho - i) * pos--;
  25.         if (pos < 2)
  26.             pos = 9;
  27.     }
  28.     resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
  29.     if (resultado != digitos.charAt(0)){
  30.         return false;
  31.     }
  32.     tamanho = tamanho + 1;
  33.     numeros = cnpj.substring(0,tamanho);
  34.     soma = 0;
  35.     pos = tamanho - 7;
  36.     for (i = tamanho; i >= 1; i--) {
  37.         soma += numeros.charAt(tamanho - i) * pos--;
  38.         if (pos < 2)
  39.             pos = 9;
  40.     }
  41.  
  42.     resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
  43.    
  44.     return (resultado == digitos.charAt(1));
  45. }
  46.  
  47. function isCnpjFormatted(cnpj) {
  48.     var validCNPJ = /\d{2,3}.\d{3}.\d{3}\/\d{4}-\d{2}/;
  49.     return cnpj.match(validCNPJ);
  50. }
  51.  
  52.  
  53. function isCpf(cpf){
  54.     exp = /\.|-/g;
  55.     cpf = cpf.toString().replace(exp, "");
  56.     var digitoDigitado = eval(cpf.charAt(9)+cpf.charAt(10));
  57.     var soma1=0, soma2=0;
  58.     var vlr =11;
  59.     for(i=0;i<9;i++){
  60.         soma1+=eval(cpf.charAt(i)*(vlr-1));
  61.         soma2+=eval(cpf.charAt(i)*vlr);
  62.         vlr--;
  63.     }  
  64.     soma1 = (((soma1*10)%11)==10 ? 0:((soma1*10)%11));
  65.     soma2 = (((soma2+(2*soma1))*10)%11);
  66.    
  67.     if(cpf == "11111111111" || cpf == "22222222222" || cpf ==
  68.             "33333333333" || cpf == "44444444444" || cpf == "55555555555" || cpf ==
  69.             "66666666666" || cpf == "77777777777" || cpf == "88888888888" || cpf ==
  70.             "99999999999" || cpf == "00000000000" ){
  71.         var digitoGerado = null;
  72.     }else{
  73.         var digitoGerado = (soma1*10) + soma2;
  74.     }
  75.  
  76.     if(digitoGerado != digitoDigitado){
  77.        return false;
  78.     }
  79.     return true;
  80. }
  81. function isCpfFormatted(cpf) {
  82.     var validCPF = /^\d{3}\.\d{3}\.\d{3}\-\d{2}$/;
  83.     return cpf.match(validCPF);
  84. }
  85.  
  86. (function($) {
  87.     $.validator.addMethod("cpf", function(value, element, type) {
  88.         if (value == "")
  89.             return true;
  90.        
  91.         if ((type == 'format' || type == 'both') && !isCpfFormatted(value))
  92.             return false;
  93.         else
  94.             return ((type == 'valid' || type == 'both')) ? isCpf(value) : true;
  95.        
  96.     }, function(type,element) {
  97.         return (type == 'format' || (type == 'both' && !isCpfFormatted($(element).val()))) ?
  98.                 'Formato do CPF não é válido' : 'Por favor digite um CPF válido';
  99.     });
  100.     $.validator.addMethod("cnpj", function(value, element, type) {
  101.         if (value == "")
  102.             return true;
  103.        
  104.         if ((type == 'format' || type == 'both') && !isCnpjFormatted(value))
  105.             return false;
  106.         else
  107.             return ((type == 'valid' || type == 'both')) ? isCnpj(value) : true;
  108.        
  109.     }, function(type,element) {
  110.         return (type == 'format' || (type == 'both' && !isCnpjFormatted($(element).val()))) ?
  111.                 'Formato do CNPJ não é válido' : 'Por favor digite um CNPJ válido';
  112.     });
  113. })(jQuery);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement