Last active
June 14, 2018 14:14
-
-
Save franciscojsc/d286d85332644962cb631f7fafaeaa4c to your computer and use it in GitHub Desktop.
Validador de CPF em Csharp
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
/* | |
Fonte: | |
https://social.msdn.microsoft.com/Forums/pt-BR/1dbe81e6-c063-4ae5-ae1d-5643fb4b0e62/validar-cpf-em-c?forum=vscsharppt | |
https://forum.imasters.com.br/topic/140772-resolvido-validar-cpf/ | |
*/ | |
public static class ValidaCPF | |
{ | |
public static bool isCPF(string valor) | |
{ | |
if (valor == "") | |
return false; | |
valor = valor.Replace("-", ""); | |
valor = valor.Replace(".", ""); | |
string cpf = valor; | |
int d1, d2; | |
int soma = 0; | |
string digitado = ""; | |
string calculado = ""; | |
// Pesos para calcular o primeiro digito | |
int[] peso1 = new int[] { 10, 9, 8, 7, 6, 5, 4, 3, 2 }; | |
// Pesos para calcular o segundo digito | |
int[] peso2 = new int[] { 11, 10, 9, 8, 7, 6, 5, 4, 3, 2 }; | |
int[] n = new int[11]; | |
// Se o tamanho for < 11 entao retorna como inválido | |
if (cpf.Length != 11) | |
return false; | |
// Caso coloque todos os numeros iguais | |
switch (cpf) | |
{ | |
case "11111111111": | |
return false; | |
case "00000000000": | |
return false; | |
case "22222222222": | |
return false; | |
case "33333333333": | |
return false; | |
case "44444444444": | |
return false; | |
case "55555555555": | |
return false; | |
case "66666666666": | |
return false; | |
case "77777777777": | |
return false; | |
case "88888888888": | |
return false; | |
case "99999999999": | |
return false; | |
} | |
try | |
{ | |
// Quebra cada digito do CPF | |
n[0] = Convert.ToInt32(cpf.Substring(0, 1)); | |
n[1] = Convert.ToInt32(cpf.Substring(1, 1)); | |
n[2] = Convert.ToInt32(cpf.Substring(2, 1)); | |
n[3] = Convert.ToInt32(cpf.Substring(3, 1)); | |
n[4] = Convert.ToInt32(cpf.Substring(4, 1)); | |
n[5] = Convert.ToInt32(cpf.Substring(5, 1)); | |
n[6] = Convert.ToInt32(cpf.Substring(6, 1)); | |
n[7] = Convert.ToInt32(cpf.Substring(7, 1)); | |
n[8] = Convert.ToInt32(cpf.Substring(8, 1)); | |
n[9] = Convert.ToInt32(cpf.Substring(9, 1)); | |
n[10] = Convert.ToInt32(cpf.Substring(10, 1)); | |
} | |
catch | |
{ | |
return false; | |
} | |
// Calcula cada digito com seu respectivo peso | |
for (int i = 0; i <= peso1.GetUpperBound(0); i++) | |
{ | |
soma += (peso1[i] * Convert.ToInt32(n[i])); | |
} | |
// Pega o resto da divisao | |
int resto = soma % 11; | |
if (resto == 1 || resto == 0) | |
d1 = 0; | |
else | |
d1 = 11 - resto; | |
soma = 0; | |
// Calcula cada digito com seu respectivo peso | |
for (int i = 0; i <= peso2.GetUpperBound(0); i++) | |
{ | |
soma += (peso2[i] * Convert.ToInt32(n[i])); | |
} | |
// Pega o resto da divisao | |
resto = soma % 11; | |
if (resto == 1 || resto == 0) | |
d2 = 0; | |
else | |
d2 = 11 - resto; | |
calculado = d1.ToString() + d2.ToString(); | |
digitado = n[9].ToString() + n[10].ToString(); | |
// Se os ultimos dois digitos calculados bater com | |
// os dois ultimos digitos do cpf entao é válido | |
if (calculado == digitado) | |
return (true); | |
else | |
return (false); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment