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:
- Comparison of characters must be in the same case. Don't compare 'D' to 'a'.
- The position will start at zero. For example, E the fifth letter in the alphabet is at position 4.
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.
Read more...