Last active
September 18, 2025 18:03
-
-
Save pmarkun/1bf89a79eca38e15ef2bd14b7d39ca53 to your computer and use it in GitHub Desktop.
validarCPF.js
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
| /** | |
| * @param {Payload} payload - Description of the payload parameter. | |
| * @returns {boolean} whether the CPF é válido. | |
| */ | |
| export default function (payload) { | |
| const cpf = String(payload.submission.submissions.cpf); | |
| if (!cpf) return false; | |
| // Remove caracteres não numéricos | |
| const cleaned = cpf.replace(/\D/g, ""); | |
| // Precisa ter 11 dígitos | |
| if (cleaned.length !== 11) return false; | |
| // Elimina CPFs com todos os dígitos iguais (ex: 11111111111) | |
| if (/^(\d)\1{10}$/.test(cleaned)) return false; | |
| // Função para calcular dígito verificador | |
| const calcCheckDigit = (base, factor) => { | |
| let total = 0; | |
| for (let i = 0; i < base.length; i++) { | |
| total += parseInt(base.charAt(i), 10) * (factor - i); | |
| } | |
| const resto = (total * 10) % 11; | |
| return resto === 10 ? 0 : resto; | |
| }; | |
| // Calcula e valida os dois dígitos | |
| const firstNine = cleaned.substring(0, 9); | |
| const firstCheck = calcCheckDigit(firstNine, 10); | |
| if (firstCheck !== parseInt(cleaned.charAt(9), 10)) return false; | |
| const firstTen = cleaned.substring(0, 10); | |
| const secondCheck = calcCheckDigit(firstTen, 11); | |
| if (secondCheck !== parseInt(cleaned.charAt(10), 10)) return false; | |
| return true; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment