Click here to Skip to main content
12,764,784 members (36,309 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

18.8K views
13 bookmarked
Posted 20 May 2014

IBAN Validator

, 22 May 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
Quick&Dirty static method for IBAN code validation

Introduction

For those who need a small, compact, working out of the box IBAN Validator without all the bells and whistles.

Background

Very good article about IBAN validating here on CodeProject:

http://www.codeproject.com/Articles/55667/IBAN-Verification-in-C-Excel-Automation-Add-in-Wor

Using the code

Just put this static method wherever you need it and invoke it on the string containing the iban.

Retruns TRUE only on a valid IBAN account number.

public static bool ValidateBankAccount(string bankAccount)
        { 
            bankAccount = bankAccount.ToUpper(); //IN ORDER TO COPE WITH THE REGEX BELOW
            if (String.IsNullOrEmpty(bankAccount))
                return false;
            else if (System.Text.RegularExpressions.Regex.IsMatch(bankAccount, "^[A-Z0-9]"))
            {
                bankAccount = bankAccount.Replace(" ", String.Empty);
                string bank =
                bankAccount.Substring(4, bankAccount.Length - 4) + bankAccount.Substring(0, 4);
                int asciiShift = 55;
                StringBuilder sb = new StringBuilder();
                foreach (char c in bank)
                {
                    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;
        } 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

sirol81
Spain Spain
No Biography provided

You may also be interested in...

Comments and Discussions

 
GeneralMy vote of 5 Pin
Prasad Khandekar22-May-14 5:01
professionalPrasad Khandekar22-May-14 5:01 
QuestionGreat but Pin
Member 1063000821-May-14 23:34
memberMember 1063000821-May-14 23:34 
AnswerRe: Great but Pin
sirol8122-May-14 3:30
membersirol8122-May-14 3:30 
GeneralRe: Great but Pin
Karri Kalpio25-May-14 21:54
memberKarri Kalpio25-May-14 21:54 
GeneralMy vote of 5 Pin
Volynsky Alex21-May-14 10:54
professionalVolynsky Alex21-May-14 10:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170217.1 | Last Updated 22 May 2014
Article Copyright 2014 by sirol81
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid