Click here to Skip to main content
15,794,236 members
Articles / Programming Languages / C#

IBAN Validator

Rate me:
Please Sign up or sign in to vote.
5.00/5 (6 votes)
22 May 2014CPOL 68.7K   13   12
Quick&Dirty static method for IBAN code validation


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


Very good article about IBAN validating here on CodeProject:

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());
                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;
                return false;


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

Written By
Spain Spain
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

BugThe regular expression doesn't work as expected. Pin
VahanHakobyan27-Mar-20 4:49
VahanHakobyan27-Mar-20 4:49 
SuggestionNice! Pin
beepoman6-Mar-19 0:04
beepoman6-Mar-19 0:04 
PraiseThanks for all the comments Pin
sirol8130-Jan-19 2:43
sirol8130-Jan-19 2:43 
QuestionExtremely simple validation number calculation for digit-only account numbers Pin
4Bytes16-Dec-18 12:11
4Bytes16-Dec-18 12:11 
QuestionISO20022 Pin
proximuss30-Apr-18 6:06
proximuss30-Apr-18 6:06 
QuestionGreat! I made it simpler... Pin
Ton_B16-Mar-17 6:29
Ton_B16-Mar-17 6:29 
AnswerRe: Great! I made it simpler... Pin
Yves Goergen7-Jul-20 11:51
Yves Goergen7-Jul-20 11:51 
GeneralMy vote of 5 Pin
Prasad Khandekar22-May-14 5:01
professionalPrasad Khandekar22-May-14 5:01 
QuestionGreat but Pin
Menci Lucio21-May-14 23:34
professionalMenci Lucio21-May-14 23:34 
you wrote:
System.[...].IsMatch(bankAccount, "^[A-Z0-9]")

It means that the first one character is A-Z or 0-9

I think you should write
! System.[...].IsMatch(bankAccount, "[^A-Za-z0-9 ]")

It means that if not (!) exists a character that is A-Z, 0-9 OR SPACE (two line after you wrote Replace(" ","")
AnswerRe: Great but Pin
sirol8122-May-14 3:30
sirol8122-May-14 3:30 
GeneralRe: Great but Pin
Karri Kalpio25-May-14 21:54
Karri 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.