Skip to content

Instantly share code, notes, and snippets.

@httpmurilo
Created September 11, 2020 12:05
Show Gist options
  • Save httpmurilo/2492040e518f3cdf77bba1e3fdc864b6 to your computer and use it in GitHub Desktop.
Save httpmurilo/2492040e518f3cdf77bba1e3fdc864b6 to your computer and use it in GitHub Desktop.
Valida Cpf
bool isCpfMurilo(string cpf)
{
var valor = cpf.Replace(".", "");
valor = valor.Replace("-", "");
var igual = true;
for (var i = 1; i < 11 && igual; i++)
if (valor[i] != valor[0])
igual = false;
var numeros = new int[11];
for (var i = 0; i < 11; i++)
numeros[i] = int.Parse(
valor[i].ToString());
var soma = 0;
for (var i = 0; i < 9; i++)
soma += (10 - i) * numeros[i];
var resultado = soma % 11;
if (resultado == 1 || resultado == 0)
{
if (numeros[9] != 0)
return false;
}
else if (numeros[9] != 11 - resultado)
return false;
soma = 0;
for (var i = 0; i < 10; i++)
soma += (11 - i) * numeros[i];
resultado = soma % 11;
if (resultado == 1 || resultado == 0)
{
if (numeros[10] != 0)
return false;
}
else if (numeros[10] != 11 - resultado)
return false;
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment