Friday, August 10, 2007

Java IBAN check digit validation

The International Bank Account Number (IBAN) is an international standard for identifying bank accounts across national borders. It was originally adopted by the European Committee for Banking Standards, and was later adopted as an ISO standard (ISO 13616). Although currently only used in Europe, where it was adopted to facilitate cross border payments, it is anticipated that IBAN will a more global standard in a few years.

A quick search on the web for IBAN will show up information on the standard, including some web based validators. Since I could not find one, I have just put together a Java implementation of the IBAN check digit validation algorithm based on ECBS IBAN standard v 3.2. Code , including test cases, are available here. Please note that this works with the 'electronic format' (no spaces) of IBAN and not the 'paper format'.

The code is reasonably straight forward, the challenge I had was to elegantly determine the appropriate numeric value of a letter as per section 6.3 (Alpha to Numeric conversion). In the end I found the simplest way was to compare the given character to 'A'. This gave me the position of the character in the alphabet. Two key points though:

  1. Comparison of characters must be in the same case. Don't compare 'D' to 'a'.
  2. The position will start at zero. For example, E the fifth letter in the alphabet is at position 4.
Here is the code snippet for that:


private static int getAlphabetPosition(char letter) {
return Character.valueOf(
Character.toUpperCase(letter))
.compareTo(Character.valueOf('A')
);
}

Of course once you have the position of a letter in the alphabet don't forget to add 10 to the result to get the numeric value as per IBAN standard.

In the implementation I have included an IBAN.properties which contains the lengths for all the country codes currently signed up to the IBAN initiative. You could take this forward by adding in the formatting patterns that exist for each country and include this in the validation too.

No comments:

Post a Comment