Created
December 3, 2015 18:58
-
-
Save asfelix/67f7a931000023a86f19 to your computer and use it in GitHub Desktop.
script em javascript para validação de CPF
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function cpf() { | |
| var cpf = prompt('Digite seu CPF: '); | |
| cpf = cpf.replace(/[^\d]+/g,''); // Remove qualquer dígito que não seja número | |
| if (cpf.length != 11 || cpf.length === 0){ | |
| alert("O CPF deve conter 11 dígitos numéricos"); | |
| } else if (cpf == "00000000000" || cpf == "11111111111" || cpf == "22222222222" || cpf == "33333333333" || cpf == "44444444444" || cpf == "55555555555" || cpf == "66666666666" || cpf == "77777777777" || cpf == "88888888888" || cpf == "99999999999"){ | |
| alert("CPF inválido"); | |
| } else { | |
| add = 0; | |
| for (i=0; i < 9; i ++) | |
| add += parseInt(cpf.charAt(i)) * (10 - i); | |
| rev = 11 - (add % 11); | |
| if (rev == 10 || rev == 11) | |
| rev = 0; | |
| if (rev != parseInt(cpf.charAt(9))) | |
| alert("O CPF informado é inválido"); | |
| add = 0; | |
| for (i = 0; i < 10; i ++) | |
| add += parseInt(cpf.charAt(i)) * (11 - i); | |
| rev = 11 - (add % 11); | |
| if (rev == 10 || rev == 11) | |
| rev = 0; | |
| if (rev != parseInt(cpf.charAt(10))) | |
| alert("O CPF informado é inválido"); | |
| alert('O CPF informado é válido.');return true; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment