Example #1
0
        /// <summary>Determines whether the specified value of the object is valid.</summary>
        /// <param name="value">The value of the object to validate.</param>
        /// <returns><see langword="true"/> if the specified value is valid; otherwise, <see langword="false"/>.</returns>
        public override bool IsValid(object value)
        {
            string valueToValidate = value as string;

            if (string.IsNullOrWhiteSpace(valueToValidate))
            {
                /* This may seem strange, but in order to reject empty values the RequiredAttribute should be used */
                return(true);
            }

            /* Then, check if the account number matches the regular expression */
            Match match = new IbanValidatorRegex().Match(valueToValidate);

            if (!match.Success)
            {
                return(false);
            }

            /* If the account number passes the regex check, continue to check the contents */
            string countryCode = match.Groups["countrycode"].Value;
            int    checkDigits = int.Parse(match.Groups["check_digits"].Value, CultureInfo.InvariantCulture);
            string accountId   = match.Groups["accountid"].Value;

            BigInteger working   = ConvertToNumeric(accountId + countryCode + checkDigits);
            BigInteger remainder = working % 97;
            bool       isValid   = remainder == 1;

            return(isValid);
        }
Example #2
0
        /// <summary>Implements the validation logic for the receiver.</summary>
        /// <param name="objectToValidate">The object to validate.</param>
        /// <param name="currentTarget">The object on the behalf of which the validation is performed.</param>
        /// <param name="key">The key that identifies the source of objectToValidate.</param>
        /// <param name="validationResults">The validation results to which the outcome of the validation should be stored.</param>
        protected override void DoValidate(string objectToValidate, object currentTarget, string key, ValidationResults validationResults)
        {
            /* First, check if the account number has a value. */
            if (string.IsNullOrEmpty(objectToValidate))
            {
                if (this.Negated)
                {
                    this.LogValidationResult(validationResults, this.GetMessage(objectToValidate, key), currentTarget, key);
                }

                return;
            }

            /* Then, check if the account number matches the regular expression */
            Match match = new IbanValidatorRegex().Match(objectToValidate);

            if (!match.Success && !this.Negated)
            {
                this.LogValidationResult(validationResults, this.GetMessage(objectToValidate, key), currentTarget, key);
                return;
            }
            else if (!match.Success && this.Negated)
            {
                return;
            }

            /* If the account number passes the regex check, continue to check the contents */
            string countryCode = match.Groups["countrycode"].Value;
            int    checkDigits = int.Parse(match.Groups["check_digits"].Value, CultureInfo.InvariantCulture);
            string accountId   = match.Groups["accountid"].Value;

            BigInteger working   = ConvertToNumeric(accountId + countryCode + checkDigits);
            BigInteger remainder = working % 97;
            bool       isValid   = remainder == 1;

            isValid = this.Negated ? !isValid : isValid;

            if (!isValid)
            {
                this.LogValidationResult(validationResults, this.GetMessage(objectToValidate, key), currentTarget, key);
            }
        }