Skip to content

Instantly share code, notes, and snippets.

@joaohcrangel
Last active October 14, 2025 00:37
Show Gist options
  • Select an option

  • Save joaohcrangel/8bd48bcc40b9db63bef7201143303937 to your computer and use it in GitHub Desktop.

Select an option

Save joaohcrangel/8bd48bcc40b9db63bef7201143303937 to your computer and use it in GitHub Desktop.
Função para validar CPF em TypeScript
function isValidCPF(value: string) {
if (typeof value !== 'string') {
return false;
}
value = value.replace(/[^\d]+/g, '');
if (value.length !== 11 || !!value.match(/(\d)\1{10}/)) {
return false;
}
const values = value.split('').map(el => +el);
const rest = (count) => (values.slice(0, count-12).reduce( (soma, el, index) => (soma + el * (count-index)), 0 )*10) % 11 % 10;
return rest(10) === values[9] && rest(11) === values[10];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment