Beispiel #1
0
        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            if (value == null)
            {
                return(null);
            }

            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 digito, tempCnpj, CNPJ;

            CNPJ = value.ToString().Trim();
            CNPJ = CNPJ.Replace(".", "").Replace("-", "").Replace("/", "");

            if (CNPJ.Length != 14)
            {
                return(new ValidationResult("CNPJ Inválido."));
            }

            tempCnpj = CNPJ.Substring(0, 12);
            soma     = 0;

            for (int i = 0; i < 12; i++)
            {
                soma += int.Parse(tempCnpj[i].ToString()) * multiplicador1[i];
            }
            resto = (soma % 11);

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

            digito   = resto.ToString();
            tempCnpj = tempCnpj + digito;
            soma     = 0;

            for (int i = 0; i < 13; i++)
            {
                soma += int.Parse(tempCnpj[i].ToString()) * multiplicador2[i];
            }
            resto = (soma % 11);

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

            digito = digito + resto.ToString();

            if (CNPJ.EndsWith(digito))
            {
                return(null);
            }
            else
            {
                return(new ValidationResult("CNPJ Inválido."));
            }
        }
        public bool ValidaCNPJ()
        {
            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;
            int    resto;
            string digito;
            string tempCnpj;

            CNPJ = CNPJ.Trim();
            CNPJ = Mascara.FormatarPropriedade(CNPJ);

            if (CNPJ.Length != 14)
            {
                return(false);
            }

            tempCnpj = CNPJ.Substring(0, 12);
            soma     = 0;

            for (int i = 0; i < 12; i++)
            {
                soma += int.Parse(tempCnpj[i].ToString()) * multiplicador1[i];
            }

            resto = (soma % 11);

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

            digito   = resto.ToString();
            tempCnpj = tempCnpj + digito;
            soma     = 0;

            for (int i = 0; i < 13; i++)
            {
                soma += int.Parse(tempCnpj[i].ToString()) * multiplicador2[i];
            }

            resto = (soma % 11);

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

            digito = digito + resto.ToString();
            return(CNPJ.EndsWith(digito));
        }