Advertisement
Guest User

Untitled

a guest
Mar 18th, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function valCNPJ(cnpj) {
  2.     cnpj = cnpj.replace(/[^\d]+/g, '');
  3.  
  4.     if (cnpj == '') return false;
  5.  
  6.     if (cnpj.length != 14)
  7.         return false;
  8.  
  9.     // Elimina CNPJs invalidos conhecidos
  10.     if (cnpj == "00000000000000" ||
  11.         cnpj == "11111111111111" ||
  12.         cnpj == "22222222222222" ||
  13.         cnpj == "33333333333333" ||
  14.         cnpj == "44444444444444" ||
  15.         cnpj == "55555555555555" ||
  16.         cnpj == "66666666666666" ||
  17.         cnpj == "77777777777777" ||
  18.         cnpj == "88888888888888" ||
  19.         cnpj == "99999999999999")
  20.         return false;
  21.  
  22.     // Valida DVs
  23.     tamanho = cnpj.length - 2
  24.     numeros = cnpj.substring(0, tamanho);
  25.     digitos = cnpj.substring(tamanho);
  26.     soma = 0;
  27.     pos = tamanho - 7;
  28.     for (i = tamanho; i >= 1; i--) {
  29.         soma += numeros.charAt(tamanho - i) * pos--;
  30.         if (pos < 2)
  31.             pos = 9;
  32.     }
  33.     resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
  34.     if (resultado != digitos.charAt(0))
  35.         return false;
  36.  
  37.     tamanho = tamanho + 1;
  38.     numeros = cnpj.substring(0, tamanho);
  39.     soma = 0;
  40.     pos = tamanho - 7;
  41.     for (i = tamanho; i >= 1; i--) {
  42.         soma += numeros.charAt(tamanho - i) * pos--;
  43.         if (pos < 2)
  44.             pos = 9;
  45.     }
  46.     resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
  47.     if (resultado != digitos.charAt(1))
  48.         return false;
  49.  
  50.     return true;
  51.  
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement