Advertisement
Guest User

Untitled

a guest
Sep 19th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.10 KB | None | 0 0
  1. // Script para validação de CPF (Cadastro de Pessoas Físicas)
  2.  
  3. // função
  4. function validaCPF(cpf) {
  5. cpf = cpf.replace(/[^\d]+/g,'');
  6. if (cpf.length != 11 || cpf == "00000000000" || cpf == "11111111111" || cpf == "22222222222" || cpf == "33333333333" || cpf == "44444444444" || cpf == "55555555555" || cpf == "66666666666" || cpf == "77777777777" || cpf == "88888888888" || cpf == "99999999999") return false;
  7.  
  8. var soma = 0, resto;
  9.  
  10. for (i=1; i<=9; i++) soma = soma + parseInt(cpf.substring(i-1, i)) * (11 - i);
  11. resto = (soma * 10) % 11;
  12. if ((resto == 10) || (resto == 11)) resto = 0;
  13. if (resto != parseInt(cpf.substring(9, 10))) return false;
  14.  
  15. soma = 0;
  16.  
  17. for (i = 1; i <= 10; i++) soma = soma + parseInt(cpf.substring(i-1, i)) * (12 - i);
  18. resto = (soma * 10) % 11;
  19. if ((resto == 10) || (resto == 11)) resto = 0;
  20. if (resto != parseInt(cpf.substring(10, 11))) return false;
  21.  
  22. return true;
  23. }
  24.  
  25. // demo
  26. var cpfExemplos = ['123.456.789-00','048.647.139-01', '035.068.383-21', '035.068.383-26', '000.000.000-00', '436.733.302-76'];
  27. cpfExemplos.forEach((cpf) => console.log(`${cpf}: ${validaCPF(cpf)}`));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement