TrabalhosGratuitos.com - Trabalhos, Monografias, Artigos, Exames, Resumos de livros, Dissertações
Pesquisar

Validação de cpf em c#

Por:   •  25/11/2015  •  Trabalho acadêmico  •  932 Palavras (4 Páginas)  •  371 Visualizações

Página 1 de 4

public static bool ValidaCPF(string vrCPF) { string valor = vrCPF.Replace(".", ""); valor = valor.Replace("-", ""); if (valor.Length != 11) return false; bool igual = true; for (int i = 1; i < 11 && igual; i++) if (valor[i] != valor[0]) igual = false; if (igual || valor == "12345678909") return false; int[] numeros = new int[11]; for (int i = 0; i < 11; i++) numeros[i] = int.Parse( valor[i].ToString()); int soma = 0; for (int i = 0; i < 9; i++) soma += (10 - i) * numeros[i]; int 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 (int 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; }

Leia mais em: Validação de CPF e CNPJ http://www.devmedia.com.br/validacao-de-cpf-e-cnpj/3950#ixzz3pzxoNbEl

namespace ValidacoesLibrary { public class Validacoes { public static bool ValidaCPF(string vrCPF) { string valor = vrCPF.Replace(".", ""); valor = valor.Replace("-", ""); if (valor.Length != 11) return false; bool igual = true; for (int i = 1; i < 11 && igual; i++) if (valor[i] != valor[0]) igual = false; if (igual || valor == "12345678909") return false; int[] numeros = new int[11]; for (int i = 0; i < 11; i++) numeros[i] = int.Parse( valor[i].ToString()); int soma = 0; for (int i = 0; i < 9; i++) soma += (10 - i) * numeros[i]; int 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 (int 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; } } }

Leia mais em: Validação de CPF e CNPJ http://www.devmedia.com.br/validacao-de-cpf-e-cnpj/3950#ixzz3sXnYRj4O

namespace ValidacoesLibrary { public class Validacoes { public static bool ValidaCPF(string vrCPF) { string valor = vrCPF.Replace(".", ""); valor = valor.Replace("-", ""); if (valor.Length != 11) return false; bool igual = true; for (int i = 1; i < 11 && igual; i++) if (valor[i] != valor[0]) igual = false; if (igual || valor == "12345678909") return false; int[] numeros = new int[11]; for (int i = 0; i < 11; i++) numeros[i] = int.Parse( valor[i].ToString()); int soma = 0; for (int i = 0; i < 9; i++) soma += (10 - i) * numeros[i]; int 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 (int 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; } } }

Leia mais em: Validação de CPF e CNPJ http://www.devmedia.com.br/validacao-de-cpf-e-cnpj/3950#ixzz3sXnYRj4O

namespace ValidacoesLibrary

...

Baixar como (para membros premium)  txt (5.1 Kb)   pdf (45.4 Kb)   docx (10.5 Kb)  
Continuar por mais 3 páginas »
Disponível apenas no TrabalhosGratuitos.com