/// <summary> /// Checks if a string value is a valid CPF representation /// </summary> /// <param name="value">a CPF string to be checked</param> /// <param name="punctuation">the punctuation setting to /// how validation must be handled</param> /// <returns>true if CPF string is valid; false otherwise</returns> internal static bool Validate(string value, CpfPunctuation punctuation) { if (StringHelper.IsNullOrWhiteSpace(value)) { return(false); } if (!Regex.IsMatch(value, CpfHelper.regexValidations[punctuation])) { return(false); } value = CpfHelper.Sanitize(value); if (CpfHelper.IsSameDigit(value)) { return(false); } var inputDigit1 = int.Parse( value.Substring(9, 1), CultureInfo.InvariantCulture); var inputDigit2 = int.Parse( value.Substring(10, 1), CultureInfo.InvariantCulture); var calcDigit1 = CpfHelper.CreateChecksum(value.Substring(0, 9)); var calcDigit2 = CpfHelper.CreateChecksum(value.Substring(0, 10)); return(inputDigit1 == calcDigit1 && inputDigit2 == calcDigit2); }
/// <summary> /// Initializes a new instance of the <see cref="Cpf"/> class. /// </summary> /// <param name="value">a valid CPF string.</param> /// <param name="punctuation">the punctuation setting to /// how validation must be handled.</param> public Cpf(string value, CpfPunctuation punctuation) { if (StringHelper.IsNullOrWhiteSpace(value)) { throw new ArgumentException("O CPF não pode ser nulo ou branco"); } if (!CpfHelper.Validate(value, punctuation)) { throw new ArgumentException("O CPF não é válido"); } this.parsedValue = CpfHelper.Sanitize(value); this.Punctuation = punctuation; }
/// <summary> /// Tries to create a new instance of <see cref="Cpf"/> from a CPF string. /// </summary> /// <param name="value">a CPF string.</param> /// <param name="cpf">the new instance of <see cref="Cpf"/>.</param> /// <param name="punctuation">the punctuation setting to /// how validation must be handled.</param> /// <returns>true if CPF string is valid; false, otherwise.</returns> public static bool TryParse(string value, out Cpf cpf, CpfPunctuation punctuation) { var parsed = false; try { cpf = new Cpf(value, punctuation); parsed = true; } catch (ArgumentException) { cpf = null; parsed = false; } return(parsed); }
/// <summary> /// Initializes a new instance of the CpfAttribute class. /// </summary> /// <param name="errorMessage">The error message to associate with a validation control if validation fails.</param> /// <param name="punctuation">Indicates how punctuation will be handled.</param> public CpfAttribute(string errorMessage, CpfPunctuation punctuation) : base(errorMessage) { this.punctuation = punctuation; }
/// <summary> /// Creates a new instance of <see cref="Cpf"/> from a CPF string. /// </summary> /// <param name="value">a CPF string.</param> /// <param name="punctuation">the punctuation setting to /// how validation must be handled.</param> /// <returns>the new instance of <see cref="Cpf"/>.</returns> public static Cpf Parse(string value, CpfPunctuation punctuation) { return(new Cpf(value, punctuation)); }
/// <summary> /// Checks if a string value is a valid CPF representation. /// </summary> /// <param name="value">a CPF string to be checked.</param> /// <param name="punctuation">the punctuation setting to /// how validation must be handled.</param> /// <returns>true if CPF string is valid; otherwise, false.</returns> public static bool Validate(string value, CpfPunctuation punctuation) { return(CpfHelper.Validate(value, punctuation)); }
/// <summary> /// Checks if a string value is a valid CPF representation. /// </summary> /// <param name="value">a CPF string to be checked.</param> /// <param name="punctuation">the punctuation setting to /// how validation must be handled.</param> /// <returns>true if CPF string is valid; false otherwise.</returns> internal static bool Validate(string value, CpfPunctuation punctuation) { var isValid = false; if (value == null) { return(isValid); } if (punctuation == CpfPunctuation.Strict) { isValid = value.Length == 14 && value[3] == '.' && value[7] == '.' && value[11] == '-'; } else { isValid = value.Length == 11 || value.Length == 14; } var index1 = 0; var index2 = 0; var sum1 = 0; var sum2 = 0; var sameDigits = true; char previousSymbol = ' '; for (var i = 0; isValid && i < value.Length; i++) { var symbol = value[i]; if (symbol == '-' || symbol == '.') { continue; } if (char.IsDigit(symbol)) { if (previousSymbol != ' ' && symbol != previousSymbol) { sameDigits = false; } previousSymbol = symbol; if (index1 < 9) { sum1 += (symbol - 48) * (10 - index1); index1++; } if (index2 < 10) { sum2 += (symbol - 48) * (11 - index2); index2++; } isValid = true; } else { isValid = false; } } isValid = isValid && !sameDigits; if (isValid) { var lastDigit1 = value[value.Length - 2] - 48; var lastDigit2 = value[value.Length - 1] - 48; var checksum1 = 11 - (sum1 % 11); checksum1 = checksum1 == 10 || checksum1 == 11 ? 0 : checksum1; var checksum2 = 11 - (sum2 % 11); checksum2 = checksum2 == 10 || checksum2 == 11 ? 0 : checksum2; isValid = checksum1 == lastDigit1 && checksum2 == lastDigit2; } return(isValid); }