Esempio n. 1
0
        public bool verificaCNPJ(string cnpj)
        {
            if (string.IsNullOrEmpty(cnpj))
            {
                return(false);
            }
            else
            {
                int[]  d = new int[14];
                int[]  v = new int[2];
                int    j, i, soma;
                string Sequencia, SoNumero;

                SoNumero = Regex.Replace(cnpj, "[^0-9]", string.Empty);

                //verificando se todos os numeros são iguais
                if (new string(SoNumero[0], SoNumero.Length) == SoNumero)
                {
                    return(false);
                }
                if (SoNumero.Length == 14)
                {
                    Sequencia = "6543298765432";
                    for (i = 0; i <= 13; i++)
                    {
                        d[i] = Convert.ToInt32(SoNumero.Substring(i, 1));
                    }
                    for (i = 0; i <= 1; i++)
                    {
                        soma = 0;
                        for (j = 0; j <= 11 + i; j++)
                        {
                            soma += d[j] * Convert.ToInt32(Sequencia.Substring(j + 1 - i, 1));
                        }

                        v[i] = (soma * 10) % 11;
                        if (v[i] == 10)
                        {
                            v[i] = 0;
                        }
                    }
                    return(v[0] == d[12] & v[1] == d[13]);
                }
                // CPF ou CNPJ inválido se
                // a quantidade de dígitos numérios for diferente de 11 e 14
                else
                {
                    return(false);
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Método único de validação do CPF e CNPJ, o método recebe o valor a ser validado realizar o replace dos campos não numéricos caso houver.
        /// Em seguida realiza a validação da string passada verificando se atende ao padrão definido pela Receita Federal do Brasil.
        /// </summary>
        /// <param name="cpfcnpj"></param>
        /// <param name="field"></param>
        /// <returns type=string></returns>
        public bool ValidateCPFCNPJ(string cpfcnpj, string field = "")
        {
            if (string.IsNullOrEmpty(cpfcnpj))
            {
                return(false);
            }
            int[]  d = new int[14];
            int[]  v = new int[2];
            int    j, i, soma;
            string Sequencia, numeroDocumento;

            numeroDocumento = Regex.Replace(cpfcnpj, @"[^\d]", string.Empty);

            if (new string(numeroDocumento[0], numeroDocumento.Length) == numeroDocumento)
            {
                return(false);
            }
            switch (numeroDocumento.Length)
            {
            case 11:
                for (i = 0; i <= 10; i++)
                {
                    d[i] = Convert.ToInt32(numeroDocumento.Substring(i, 1));
                }
                for (i = 0; i <= 1; i++)
                {
                    soma = 0;
                    for (j = 0; j <= 8 + i; j++)
                    {
                        soma += d[j] * (10 + i - j);
                    }

                    v[i] = (soma * 10) % 11;
                    if (v[i] == 10)
                    {
                        v[i] = 0;
                    }
                }
                if (v[0] == d[9] || v[1] == d[10])
                {
                    return(true);
                }
                else
                {
                    return(false);
                }
                break;

            case 14:
                //Sequencia de valor para a validacao do CNPJ
                Sequencia = "6543298765432";
                for (i = 0; i <= 13; i++)
                {
                    d[i] = Convert.ToInt32(numeroDocumento.Substring(i, 1));
                }
                for (i = 0; i <= 1; i++)
                {
                    soma = 0;
                    for (j = 0; j <= 11 + i; j++)
                    {
                        soma += d[j] * Convert.ToInt32(Sequencia.Substring(j + 1 - i, 1));
                    }

                    v[i] = (soma * 10) % 11;
                    if (v[i] == 10)
                    {
                        v[i] = 0;
                    }
                }
                if (v[0] == d[12] || v[1] == d[13])
                {
                    return(true);
                }
                else
                {
                    return(false);
                }
                break;

            default:
                return(false);

                break;
            }
        }
Esempio n. 3
0
        public static bool ValidaCPF(string cpf)
        {
            int[]  d = new int[14];
            int[]  v = new int[2];
            int    j, i, soma;
            string Sequencia, SoNumero;

            SoNumero = Regex.Replace(cpf, "[^0-9]", string.Empty);

            if (new string(SoNumero[0], SoNumero.Length) == SoNumero)
            {
                return(false);
            }

            if (SoNumero.Length == 11)
            {
                for (i = 0; i <= 10; i++)
                {
                    d[i] = Convert.ToInt32(SoNumero.Substring(i, 1));
                }
                for (i = 0; i <= 1; i++)
                {
                    soma = 0;
                    for (j = 0; j <= 8 + i; j++)
                    {
                        soma += d[j] * (10 + i - j);
                    }

                    v[i] = (soma * 10) % 11;
                    if (v[i] == 10)
                    {
                        v[i] = 0;
                    }
                }
                return(v[0] == d[9] & v[1] == d[10]);
            }
            else if (SoNumero.Length == 14)
            {
                Sequencia = "6543298765432";
                for (i = 0; i <= 13; i++)
                {
                    d[i] = Convert.ToInt32(SoNumero.Substring(i, 1));
                }
                for (i = 0; i <= 1; i++)
                {
                    soma = 0;
                    for (j = 0; j <= 11 + i; j++)
                    {
                        soma += d[j] * Convert.ToInt32(Sequencia.Substring(j + 1 - i, 1));
                    }

                    v[i] = (soma * 10) % 11;
                    if (v[i] == 10)
                    {
                        v[i] = 0;
                    }
                }
                return(v[0] == d[12] & v[1] == d[13]);
            }
            else
            {
                return(false);
            }
        }
        //Verifica se o CPF ou CNPJ digitado é válido
        public static bool ValidarCPFCNPJ()
        {
            if (string.IsNullOrEmpty(_CpfCnpJ))
            {
                return(false);
            }
            else
            {
                int[]  d = new int[14];
                int[]  v = new int[2];
                int    j, i, soma;
                string Sequencia, SoNumero;

                SoNumero = Regex.Replace(_CpfCnpJ, "[^0-9]", string.Empty);

                //verificando se todos os numeros são iguais
                if (new string(SoNumero[0], SoNumero.Length) == SoNumero)
                {
                    return(false);
                }

                // se a quantidade de dígitos numérios for igual a 11
                // iremos verificar como CPF
                if (SoNumero.Length == 11)
                {
                    for (i = 0; i <= 10; i++)
                    {
                        d[i] = Convert.ToInt32(SoNumero.Substring(i, 1));
                    }
                    for (i = 0; i <= 1; i++)
                    {
                        soma = 0;
                        for (j = 0; j <= 8 + i; j++)
                        {
                            soma += d[j] * (10 + i - j);
                        }

                        v[i] = (soma * 10) % 11;
                        if (v[i] == 10)
                        {
                            v[i] = 0;
                        }
                    }
                    return(v[0] == d[9] & v[1] == d[10]);
                }
                // se a quantidade de dígitos numérios for igual a 14
                // iremos verificar como CNPJ
                else if (SoNumero.Length == 14)
                {
                    Sequencia = "6543298765432";
                    for (i = 0; i <= 13; i++)
                    {
                        d[i] = Convert.ToInt32(SoNumero.Substring(i, 1));
                    }
                    for (i = 0; i <= 1; i++)
                    {
                        soma = 0;
                        for (j = 0; j <= 11 + i; j++)
                        {
                            soma += d[j] * Convert.ToInt32(Sequencia.Substring(j + 1 - i, 1));
                        }

                        v[i] = (soma * 10) % 11;
                        if (v[i] == 10)
                        {
                            v[i] = 0;
                        }
                    }
                    return(v[0] == d[12] & v[1] == d[13]);
                }
                // CPF ou CNPJ inválido se
                // a quantidade de dígitos numérios for diferente de 11 e 14
                else
                {
                    return(false);
                }
            }
        }
        public bool sValidarCpfCnpj(string cpfCnpj)
        {
            cpfCnpj = cpfCnpj.Replace(".", "");
            cpfCnpj = cpfCnpj.Replace("-", "");
            cpfCnpj = cpfCnpj.Replace("/", "");

            if (string.IsNullOrEmpty(cpfCnpj))
            {
                return(false);
            }

            else
            {
                int[] d;
                int[] v = new int[2];

                string Sequencia, soNumero;

                soNumero = Regex.Replace(cpfCnpj, "[^0-9]", string.Empty);

                if (soNumero.Length == 11)
                {
                    d = new int[11];

                    for (int i = 0; i <= 10; i++)
                    {
                        d[i] = Convert.ToInt32(soNumero.Substring(i, 1));
                    }

                    for (int i = 0; i <= 1; i++)
                    {
                        int soma = 0;

                        for (int j = 0; j <= 8 + i; j++)
                        {
                            soma += d[j] * (10 + i - j);
                        }

                        v[i] = (soma) % 11;

                        if (v[i] == 10)
                        {
                            v[i] = 0;
                        }
                    }

                    return(v[0] == d[9] & v[1] == d[10]);
                }

                else if (soNumero.Length == 14)
                {
                    d = new int[14];

                    Sequencia = "6543298765432";
                    for (int i = 0; i <= 13; i++)
                    {
                        d[i] = Convert.ToInt32(soNumero.Substring(i, 1));
                    }
                    for (int i = 0; i <= 1; i++)
                    {
                        int soma = 0;
                        for (int j = 0; j <= 11 + i; j++)
                        {
                            soma += d[j] * Convert.ToInt32(Sequencia.Substring(j + 1 - i, 1));
                        }

                        v[i] = (soma * 10) % 11;
                        if (v[i] == 10)
                        {
                            v[i] = 0;
                        }
                    }
                    return(v[0] == d[12] & v[1] == d[13]);
                }

                else
                {
                    return(false);
                }
            }
        }
Esempio n. 6
0
        public void CPFnCNPJ(string cpfcnpj)
        {
            if (!string.IsNullOrEmpty(cpfcnpj))
            {
                int[]  d = new int[14];
                int[]  v = new int[2];
                int    j, i, soma;
                string Sequencia, SoNumero;

                SoNumero = Regex.Replace(cpfcnpj, "[^0-9]", string.Empty);

                //verificando se todos os numeros são iguais
                if (new string(SoNumero[0], SoNumero.Length) == SoNumero)
                {
                    throw new Exception("CPF ou CNPJ Invalido");
                }

                // se a quantidade de dígitos numérios for igual a 11
                // iremos verificar como CPF
                if (SoNumero.Length == 11)
                {
                    for (i = 0; i <= 10; i++)
                    {
                        d[i] = Convert.ToInt32(SoNumero.Substring(i, 1));
                    }
                    for (i = 0; i <= 1; i++)
                    {
                        soma = 0;
                        for (j = 0; j <= 8 + i; j++)
                        {
                            soma += d[j] * (10 + i - j);
                        }

                        v[i] = (soma * 10) % 11;
                        if (v[i] == 10)
                        {
                            v[i] = 0;
                        }
                    }
                    if (!(v[0] == d[9] & v[1] == d[10]))
                    {
                        throw new Exception("CPF ou CNPJ Invalido");
                    }
                }
                // se a quantidade de dígitos numérios for igual a 14
                // iremos verificar como CNPJ
                else if (SoNumero.Length == 14)
                {
                    Sequencia = "6543298765432";
                    for (i = 0; i <= 13; i++)
                    {
                        d[i] = Convert.ToInt32(SoNumero.Substring(i, 1));
                    }
                    for (i = 0; i <= 1; i++)
                    {
                        soma = 0;
                        for (j = 0; j <= 11 + i; j++)
                        {
                            soma += d[j] * Convert.ToInt32(Sequencia.Substring(j + 1 - i, 1));
                        }

                        v[i] = (soma * 10) % 11;
                        if (v[i] == 10)
                        {
                            v[i] = 0;
                        }
                    }
                    if (!(v[0] == d[12] & v[1] == d[13]))
                    {
                        throw new Exception("CPF ou CNPJ Invalido");
                    }
                }
                // CPF ou CNPJ inválido se
                // a quantidade de dígitos numérios for diferente de 11 e 14
                else
                {
                    throw new Exception("CPF ou CNPJ Invalido");
                }
            }
            else
            {
                throw new Exception("Digite seu CPF/CNPJ");
            }
        }