Last active
June 19, 2019 14:52
-
-
Save YurePereira/afb504919974a3c0dac1a9ce04e426d6 to your computer and use it in GitHub Desktop.
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
| /** | |
| * | |
| * Função que faz a validação de CPF, verificando se ele está no formato certo, exemplo 'xxx.xxx.xxx-xx' ou 'xxxxxxxxxxx', e se | |
| * ele é válido, caso as duas verificação sejam verdadeiras será retornado TRUE ou FALSE caso contrario. | |
| * | |
| * @param c: String CNPJ a se validado. | |
| * @return Boolean | |
| * | |
| */ | |
| function validCPF(c) { | |
| if (c.match(/^[0-9]{3}\.?[0-9]{3}\.?[0-9]{3}\-?[0-9]{2}$/)) { | |
| c = c.replace(/(\.|\-)/g,""); | |
| var s = 0, | |
| i = 0, | |
| i2 = 0, | |
| j = 0, | |
| cpf = []; | |
| for (i = 0; i <= 8; i++) { | |
| cpf[i] = c.charAt(i); | |
| } | |
| for (i = 10; i >= 2; i--) { | |
| s += cpf[j] * i; | |
| j += 1; | |
| } | |
| cpf[9] = (s % 11 > 2) ? 11 - (s % 11) : 0; | |
| s = 0; | |
| j = 0; | |
| for (i = 11; i >= 2; i--) { | |
| s += cpf[j] * i; | |
| j += 1; | |
| } | |
| cpf[10] = (s % 11 > 2) ? 11 - (s % 11) : 0; | |
| s = 0; | |
| j = 0; | |
| return cpf[9] == c.charAt(c.length - 2) && cpf[10] == c.charAt(c.length - 1); | |
| } else { | |
| return false; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment