Exemplo n.º 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);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Completes a partial CNPJ string by appending a valid checksum trailing
        /// </summary>
        /// <param name="value">a partial CNPJ string with or without punctuation</param>
        /// <returns>a CNPJ string with a valid checksum trailing</returns>
        internal static string Complete(string value)
        {
            if (StringHelper.IsNullOrWhiteSpace(value))
            {
                throw new ArgumentException("O CNPJ é inválido");
            }

            value = CnpjHelper.Sanitize(value);

            if (!Regex.IsMatch(value, @"\d{9}"))
            {
                throw new ArgumentException("O CNPJ é inválido");
            }

            int digit1 = CnpjHelper.CreateChecksum(value, CnpjHelper.multiplier1);

            int digit2 = CnpjHelper.CreateChecksum(
                value + digit1.ToString(CultureInfo.InvariantCulture),
                CnpjHelper.multiplier2);

            return
                (value +
                 digit1.ToString(CultureInfo.InvariantCulture) +
                 digit2.ToString(CultureInfo.InvariantCulture));
        }
Exemplo n.º 3
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;
        }
Exemplo n.º 4
0
 /// <summary>
 /// Completes a partial CNPJ string by appending a valid checksum trailing
 /// </summary>
 /// <param name="value">a partial CNPJ string with or without punctuation</param>
 /// <returns>a CNPJ string with a valid checksum trailing</returns>
 public static string Complete(string value)
 {
     return(CnpjHelper.Complete(value));
 }
Exemplo n.º 5
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));
 }
Exemplo n.º 6
0
 /// <summary>
 /// Checks if a string value is a valid CNPJ representation
 /// </summary>
 /// <param name="value">a CNPJ string to be checked</param>
 /// <returns>true if CNPJ string is valid; false otherwise</returns>
 public static bool Validate(string value)
 {
     return(CnpjHelper.Validate(value, CnpjPunctuation.Loose));
 }