コード例 #1
0
        /// <summary>
        /// Parse the text to an Iban
        /// </summary>
        /// <param name="text">the text to parse</param>
        /// <returns></returns>
        public static ParsingResult TryParse(string text)
        {
            // remove whitespace chars
            var toParse = (text ?? string.Empty).Replace(" ", "").Replace("\t", "").ToUpper(CultureInfo.InvariantCulture);

            if (toParse.Length == 0)
            {
                return(new ParsingResult());
            }
            if (toParse.Length > 34)
            {
                return(ParsingResult.Invalid(Resources.Iban_MoreThan34Characters));
            }
            if (toParse.Length <= 4)
            {
                return(ParsingResult.Invalid(Resources.Iban_LessThan5Characters));
            }

            var result = new ParsingResult
            {
                CountryCode        = toParse.Substring(0, 2),
                CheckDigits        = toParse.Substring(2, 2),
                BasicAccountNumber = toParse.Substring(4, toParse.Length - 4)
            };

            if (!result.CountryCode.All(chr => char.IsUpper(chr)))
            {
                return(ParsingResult.Invalid(Resources.Iban_StartWithIso));
            }
            if (!result.CheckDigits.All(chr => char.IsDigit(chr)))
            {
                return(ParsingResult.Invalid(Resources.Iban_2CheckDigits));
            }
            if (!result.BasicAccountNumber.All(chr => char.IsLetterOrDigit(chr)))
            {
                return(ParsingResult.Invalid(Resources.Iban_OnlyLettersAndDigits));
            }

            return(result);
        }