dotnet web sql business etc

16 березня, 2011

IBAN validation: C# implementation

IBAN validation checksum (a basic ISO 7064 mod 97-10 calculation where the remainder must equal 1).

Let's recall the steps of IBAN validation:

1.Move the four initial characters to the end of the string
2.Replace the letters in the string with digits, expanding the string as necessary, such that A=10, B=11 and Z=35.
3.Convert the string to an integer and mod-97 the entire number

And below is implementation of IBAN validation written in C#:
   public static bool ValidateIBAN(string ibanValue)
        {
            if (System.Text.RegularExpressions.Regex.IsMatch(ibanValue, "^[A-Z0-9]"))
            {
                ibanValue = ibanValue.Replace(" ", String.Empty);
                string iban =

                ibanValue.Substring(4, ibanValue.Length - 4) + ibanValue.Substring(0, 4);
                int asciiShift = 55;

                StringBuilder sb = new StringBuilder();
                foreach (char c in iban)
                {
                    int v;
                    if (Char.IsLetter(c))
                    {
                        v = c - asciiShift;
                    }
                    else
                    {
                        v = int.Parse(c.ToString());
                    }
                    sb.Append(v);
                }

                string checkSumString = sb.ToString();
                int checksum = int.Parse(checkSumString.Substring(0, 1));
                for (int i = 1; i < checkSumString.Length; i++)
                {
                    int v = int.Parse(checkSumString.Substring(i, 1));
                    checksum *= 10;
                    checksum += v;
                    checksum %= 97;
                }
                return checksum == 1;
            }
            else
            {
                return false;
            }

            }

Мітки: , ,

0 коментарі(в):

Дописати коментар

Підписка на Дописати коментарі [Atom]

<< Головна сторінка