Created
February 17, 2018 19:12
-
-
Save vazgabriel/ca1266fae95a6c1d6fb63f345b826f96 to your computer and use it in GitHub Desktop.
Function to validate brazilian 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
/* | |
* Validates a CPF number | |
* @param String: cpfString // User CPF (just numbers or formated(000.000.000-00)) | |
* @return Boolean: success/false // Returns if is valid | |
* Reference and logic: https://www.devmedia.com.br/validar-cpf-com-javascript/23916 | |
* Official code: http://www.receita.fazenda.gov.br/aplicacoes/atcta/cpf/funcoes.js | |
*/ | |
function validateCpf(cpfString: string): boolean { | |
let validated = false; // Start with false | |
// Checking cpf lenght | |
if (cpfString.length !== 14 && cpfString.length !== 11) { | |
return validated; | |
} | |
// Checking for "formated cpf (000.000.000-00)" and replace "points" | |
if (cpfString.length === 14) { | |
cpfString = cpfString.replace('.', ''); | |
cpfString = cpfString.replace('.', ''); | |
cpfString = cpfString.replace('-', ''); | |
} | |
// Variables to check cpf valid (sum and rest) | |
let sum = 0, rest = 0; | |
// Checking for "null" CPF | |
if (cpfString === '00000000000') { | |
return validated; | |
} | |
// sum numbers | |
for (let i = 1; i <= 9; ++i) { | |
sum += (parseInt(cpfString.substring(i - 1, i)) * (11 - i)); | |
} | |
// Getting rest | |
rest = (sum * 10) % 11; | |
if ((rest == 10) || (rest == 11)) { | |
rest = 0; | |
} | |
if (rest != parseInt(cpfString.substring(9, 10))) { | |
return validated; | |
} | |
validated = true; | |
return validated; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment