Exemple #1
0
        /// <summary>
        /// Checks if a string value is a valid CNPJ representation
        /// </summary>
        /// <param name="value">a CNPJ string to be checked</param>
        /// <param name="punctuation">the punctuation setting to
        /// how validation must be handled</param>
        /// <returns>true if CNPJ string is valid; false otherwise</returns>
        internal static bool Validate(string value, CnpjPunctuation punctuation)
        {
            if (StringHelper.IsNullOrWhiteSpace(value))
            {
                return(false);
            }

            if (!Regex.IsMatch(value, CnpjHelper.regexValidations[punctuation]))
            {
                return(false);
            }

            value = CnpjHelper.Sanitize(value);

            var inputDigit1 = int.Parse(
                value.Substring(12, 1),
                CultureInfo.InvariantCulture);

            var inputDigit2 = int.Parse(
                value.Substring(13, 1),
                CultureInfo.InvariantCulture);

            var calcDigit1 = CnpjHelper.CreateChecksum(value.Substring(0, 12), CnpjHelper.multiplier1);
            var calcDigit2 = CnpjHelper.CreateChecksum(value.Substring(0, 13), CnpjHelper.multiplier2);

            return(inputDigit1 == calcDigit1 && inputDigit2 == calcDigit2);
        }
Exemple #2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="Cnpj"/> class.
        /// </summary>
        /// <param name="value">a valid CNPJ string</param>
        /// <param name="punctuation">the punctuation setting to
        /// how validation must be handled</param>
        public Cnpj(string value, CnpjPunctuation punctuation)
        {
            if (StringHelper.IsNullOrWhiteSpace(value))
            {
                throw new ArgumentException("O CNPJ não pode ser nulo ou branco");
            }

            if (!CnpjHelper.Validate(value, punctuation))
            {
                throw new ArgumentException("O CNPJ não é válido");
            }

            this.parsedValue = CnpjHelper.Sanitize(value);

            this.Punctuation = punctuation;
        }
Exemple #3
0
        /// <summary>
        /// Tries to create a new instance of <see cref="Cnpj"/> from a CNPJ string
        /// </summary>
        /// <param name="value">a CNPJ string</param>
        /// <param name="cnpj">the new instance of <see cref="Cnpj"/></param>
        /// <param name="punctuation">the punctuation setting to
        /// how validation must be handled</param>
        /// <returns>true if CNPJ string is valid; false, otherwise</returns>
        public static bool TryParse(string value, out Cnpj cnpj, CnpjPunctuation punctuation)
        {
            var parsed = false;

            try
            {
                cnpj = new Cnpj(value, punctuation);

                parsed = true;
            }
            catch (ArgumentException)
            {
                cnpj = null;

                parsed = false;
            }

            return(parsed);
        }
Exemple #4
0
        /// <summary>
        /// Checks if a string value is a valid CNPJ representation.
        /// </summary>
        /// <param name="value">a CNPJ string to be checked.</param>
        /// <param name="punctuation">the punctuation setting to
        /// how validation must be handled.</param>
        /// <returns>true if CNPJ string is valid; false otherwise.</returns>
        internal static bool Validate(string value, CnpjPunctuation punctuation)
        {
            var isValid = false;

            if (value == null)
            {
                return(isValid);
            }

            if (punctuation == CnpjPunctuation.Strict)
            {
                isValid =
                    value.Length == 18 &&
                    value[2] == '.' &&
                    value[6] == '.' &&
                    value[10] == '/' &&
                    value[15] == '-';
            }
            else
            {
                isValid = value.Length == 14 || value.Length == 18;
            }

            var index1 = 0;
            var index2 = 0;

            var sum1 = 0;
            var sum2 = 0;

            for (var i = 0; isValid && i < value.Length; i++)
            {
                var symbol = value[i];

                if (symbol == '-' || symbol == '.' || symbol == '/')
                {
                    continue;
                }

                if (char.IsDigit(symbol))
                {
                    if (index1 < 12)
                    {
                        sum1 += (symbol - 48) * ((index1 < 4 ? 5 : 13) - index1);
                        index1++;
                    }

                    if (index2 < 13)
                    {
                        sum2 += (symbol - 48) * ((index2 < 5 ? 6 : 14) - index2);
                        index2++;
                    }

                    isValid = true;
                }
                else
                {
                    isValid = false;
                }
            }

            if (isValid)
            {
                var lastDigit1 = value[value.Length - 2] - 48;
                var lastDigit2 = value[value.Length - 1] - 48;

                var checksum1 = sum1 % 11;
                checksum1 = (checksum1 < 2) ? 0 : 11 - checksum1;

                var checksum2 = sum2 % 11;
                checksum2 = (checksum2 < 2) ? 0 : 11 - checksum2;

                isValid =
                    checksum1 == lastDigit1 &&
                    checksum2 == lastDigit2;
            }

            return(isValid);
        }
Exemple #5
0
 /// <summary>
 /// Creates a new instance of <see cref="Cnpj"/> from a CNPJ string
 /// </summary>
 /// <param name="value">a CNPJ string</param>
 /// <param name="punctuation">the punctuation setting to
 /// how validation must be handled</param>
 /// <returns>the new instance of <see cref="Cnpj"/></returns>
 public static Cnpj Parse(string value, CnpjPunctuation punctuation)
 {
     return(new Cnpj(value, punctuation));
 }
Exemple #6
0
 /// <summary>
 /// Checks if a string value is a valid CNPJ representation
 /// </summary>
 /// <param name="value">a CNPJ string to be checked</param>
 /// <param name="punctuation">the punctuation setting to
 /// how validation must be handled</param>
 /// <returns>true if CNPJ string is valid; otherwise, false</returns>
 public static bool Validate(string value, CnpjPunctuation punctuation)
 {
     return(CnpjHelper.Validate(value, punctuation));
 }
Exemple #7
0
        /// <summary>
        /// Checks if a string value is a valid CNPJ representation
        /// </summary>
        /// <param name="value">a CNPJ string to be checked</param>
        /// <param name="punctuation">the punctuation setting to 
        /// how validation must be handled</param>
        /// <returns>true if CNPJ string is valid; false otherwise</returns>
        internal static bool Validate(string value, CnpjPunctuation punctuation)
        {
            if (string.IsNullOrWhiteSpace(value))
            {
                return false;
            }

            if (!Regex.IsMatch(value, CnpjHelper.regexValidations[punctuation]))
            {
                return false;
            }

            value = CnpjHelper.Sanitize(value);

            var inputDigit1 = Convert.ToInt32(value.Substring(12, 1));
            var inputDigit2 = Convert.ToInt32(value.Substring(13, 1));

            var calcDigit1 = CnpjHelper.CreateChecksum(value.Substring(0, 12), CnpjHelper.multiplier1);
            var calcDigit2 = CnpjHelper.CreateChecksum(value.Substring(0, 13), CnpjHelper.multiplier2);

            return inputDigit1 == calcDigit1 && inputDigit2 == calcDigit2;
        }