Ejemplo n.º 1
0
        public static bool ValidandoCpf(string CPF)
        {
            int    multiplicador1 = 10;
            int    multiplicador2 = 11;
            string temCPF;
            string Digito;
            int    Soma;
            int    Resto;

            CPF = CPF.Trim();
            CPF = CPF.Replace(".", "").Replace("-", "");
            if (CPF.Length != 11)
            {
                return(false);
            }
            temCPF = CPF.Substring(0, 9);
            Soma   = 0;
            for (int i = 0; i < 9; i++)
            {
                Soma += int.Parse(temCPF[i].ToString()) * (multiplicador1 - i);
            }
            Resto = Soma % 11;
            if (Resto < 2)
            {
                Resto = 0;
            }
            else
            {
                Resto = 11 - Resto;
            }
            Digito = Resto.ToString();
            temCPF = temCPF + Digito;
            Soma   = 0;
            for (int i = 0; i < 10; i++)
            {
                Soma += int.Parse(temCPF[i].ToString()) * (multiplicador2 - i);
            }
            Resto = Soma % 11;
            if (Resto < 2)
            {
                Resto = 0;
            }
            else
            {
                Resto = 11 - Resto;
            }
            Digito = Digito + Resto.ToString();
            return(CPF.EndsWith(Digito));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Verifica se o CPF é válido.
        /// </summary>
        /// <param name="cpf">Dado a ser verificado.</param>
        /// <returns>TRUE se o CPF é válido ou FALSE caso ele seja inválido.</returns>
        public static bool Verificar(string cpf)
        {
            //Limpa a formatação da String
            cpf.Trim();
            cpf = cpf.Replace(".", "").Replace("-", "");

            if (long.TryParse(cpf, out long somenteNumero) == false)
            {
                return(false);
            }

            //Se o CPF não tem 11 digitos ele é inválido
            if (cpf.Length != 11)
            {
                return(false);
            }

            //Cpf válidos, porém não utilizados.
            if (cpf == "00000000000000" ||
                cpf == "11111111111111" ||
                cpf == "22222222222222" ||
                cpf == "33333333333333" ||
                cpf == "44444444444444" ||
                cpf == "55555555555555" ||
                cpf == "66666666666666" ||
                cpf == "77777777777777" ||
                cpf == "88888888888888" ||
                cpf == "99999999999999")
            {
                return(false);
            }

            int[] Multiplicador1 = new int[9] {
                10, 9, 8, 7, 6, 5, 4, 3, 2
            };

            int[] Multiplicador2 = new int[10] {
                11, 10, 9, 8, 7, 6, 5, 4, 3, 2
            };

            string DigitosCpf, DigitoVerificador;
            int    Soma = 0, Resto;

            //Gera uma string sem os dígitos verificadores
            DigitosCpf = cpf.Substring(0, 9);

            for (int i = 0; i < 9; i++)
            {
                Soma += int.Parse(DigitosCpf[i].ToString()) * Multiplicador1[i];
            }

            Resto = Soma % 11;

            if (Resto < 2)
            {
                Resto = 0;
            }
            else
            {
                Resto = 11 - Resto;
            }

            DigitoVerificador = Resto.ToString();

            DigitosCpf += DigitoVerificador;

            Soma = 0;

            for (int i = 0; i < 10; i++)
            {
                Soma += int.Parse(DigitosCpf[i].ToString()) * Multiplicador2[i];
            }

            Resto = Soma % 11;

            if (Resto < 2)
            {
                Resto = 0;
            }
            else
            {
                Resto = 11 - Resto;
            }

            DigitoVerificador += Resto.ToString();

            //Compara se DigitoVerificador é igual ao foi recebido como parâmetro pelo método e retorna um booleano
            return(cpf.EndsWith(DigitoVerificador));
        }
        /// <summary>
        /// Verifica se o CNPJ é válido.
        /// </summary>
        /// <param name="cnpj">Dado a ser verificado.</param>
        /// <returns>TRUE se o CNPJ é válido ou FALSE caso seja inválido.</returns>
        public static bool Verificar(string cnpj)
        {
            //Limpa a formatação da String
            cnpj = cnpj.Trim();
            cnpj = cnpj.Replace(".", "").Replace("-", "").Replace("/", "");

            //Verifica se existem caracteres diferentes de números
            if (long.TryParse(cnpj, out long somenteNumero) == false)
            {
                return(false);
            }

            //Se o CNPJ não tem 14 digitos ele é inválido
            if (cnpj.Length != 14)
            {
                return(false);
            }

            //Cnpj válidos, porém não utilizados.
            if (cnpj == "00000000000000" ||
                cnpj == "11111111111111" ||
                cnpj == "22222222222222" ||
                cnpj == "33333333333333" ||
                cnpj == "44444444444444" ||
                cnpj == "55555555555555" ||
                cnpj == "66666666666666" ||
                cnpj == "77777777777777" ||
                cnpj == "88888888888888" ||
                cnpj == "99999999999999")
            {
                return(false);
            }

            int[] Multiplicador1 = new int[12] {
                5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2
            };
            int[] Multiplicador2 = new int[13] {
                6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2
            };
            int    Soma, Resto;
            string DigitoVerificador, DigitosCnpj;

            //Gera uma string sem os dígitos verificadores
            DigitosCnpj = cnpj.Substring(0, 12);

            Soma = 0;
            for (int i = 0; i < 12; i++)
            {
                Soma += int.Parse(DigitosCnpj[i].ToString()) * Multiplicador1[i];
            }

            Resto = (Soma % 11);
            if (Resto < 2)
            {
                Resto = 0;
            }
            else
            {
                Resto = 11 - Resto;
            }

            DigitoVerificador = Resto.ToString();

            DigitosCnpj = DigitosCnpj + DigitoVerificador;
            Soma        = 0;
            for (int i = 0; i < 13; i++)
            {
                Soma += int.Parse(DigitosCnpj[i].ToString()) * Multiplicador2[i];
            }

            Resto = (Soma % 11);
            if (Resto < 2)
            {
                Resto = 0;
            }
            else
            {
                Resto = 11 - Resto;
            }

            DigitoVerificador += Resto.ToString();

            //Compara se DigitoVerificador é igual ao foi recebido como parâmetro pelo método e retorna um booleano
            return(cnpj.EndsWith(DigitoVerificador));
        }
Ejemplo n.º 4
0
        public static bool ValidarCPF(string Cpf)
        {
            int[] Multiplicador1 = new int[9] {
                10, 9, 8, 7, 6, 5, 4, 3, 2
            };
            int[] Multiplicador2 = new int[10] {
                11, 10, 9, 8, 7, 6, 5, 4, 3, 2
            };
            string TempCpf;
            string Digito;
            int    Soma;
            int    Resto;

            Cpf = Cpf.Trim();
            Cpf = Cpf.Replace(".", "").Replace("-", "");

            if (Cpf.Length != 11)
            {
                return(false);
            }

            TempCpf = Cpf.Substring(0, 9);
            Soma    = 0;

            for (int i = 0; i < 9; i++)
            {
                Soma += int.Parse(TempCpf[i].ToString()) * Multiplicador1[i];
            }

            Resto = Soma % 11;
            if (Resto < 2)
            {
                Resto = 0;
            }
            else
            {
                Resto = 11 - Resto;
            }

            Digito = Resto.ToString();

            TempCpf = TempCpf + Digito;

            Soma = 0;
            for (int i = 0; i < 10; i++)
            {
                Soma += int.Parse(TempCpf[i].ToString()) * Multiplicador2[i];
            }

            Resto = Soma % 11;
            if (Resto < 2)
            {
                Resto = 0;
            }
            else
            {
                Resto = 11 - Resto;
            }

            Digito = Digito + Resto.ToString();

            return(Cpf.EndsWith(Digito));
        }