Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Script para validação de CPF (Cadastro de Pessoas Físicas)
- // função
- function validaCPF(cpf) {
- cpf = cpf.replace(/[^\d]+/g,'');
- 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;
- var soma = 0, resto;
- for (i=1; i<=9; i++) soma = soma + parseInt(cpf.substring(i-1, i)) * (11 - i);
- resto = (soma * 10) % 11;
- if ((resto == 10) || (resto == 11)) resto = 0;
- if (resto != parseInt(cpf.substring(9, 10))) return false;
- soma = 0;
- for (i = 1; i <= 10; i++) soma = soma + parseInt(cpf.substring(i-1, i)) * (12 - i);
- resto = (soma * 10) % 11;
- if ((resto == 10) || (resto == 11)) resto = 0;
- if (resto != parseInt(cpf.substring(10, 11))) return false;
- return true;
- }
- // demo
- 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'];
- cpfExemplos.forEach((cpf) => console.log(`${cpf}: ${validaCPF(cpf)}`));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement