IBAN number processing
The following code has been composed after reading the "IBAN: International bank account number" of the European committee for banking standards (ECBS, http://www.ecbs.org/) (EBS204 V3.2 - August 2003).
The basic use of this module is to verify that a given IBAN number is correct.
There are also included some functions to generate IBAN from a given BBAN number, but you must always have in mind that it may lead to errors because the BBAN number cannot always be derived from the domestic account number, as ECBS warns. Please read all ECBS warnings at http://www.ecbs.org/iban/iban.htm
Namespace Financial
Public Module IBAN
Public Function IsValid(ByVal IBAN As String) As Boolean
'Preliminary step: If the IBAN is in paper format, convert to basic format by deleting all non-alphanumeric characters.
Dim PurifiedIBAN As String = GetPurifiedIBAN(IBAN)
'Step 1: Move the first four characters of the IBAN to the right of the number
Dim SwapedIBAN As String = GetSwapedIBAN(PurifiedIBAN)
'Step 2: Convert the letters into numerics in accordance with the convertion table
Dim NumericIBAN As String = GetNumericIBAN(SwapedIBAN)
'Step 3: Apply MOD 97-10 (See ISO 7064). For the check digits to be correct, the ramainder after calculating the modulus 97 must be 1.
If Mod9710(NumericIBAN) = 1 Then
Return True
Else
Return False
End If
End Function
Public Function GetPurifiedIBAN(ByVal GenericIBAN As String) As String
Dim ResultString As String = String.Empty
For Counter As Integer = 1 To GenericIBAN.Length
Dim CurrentCharacter As Char = Mid(GenericIBAN, Counter, 1)
If Char.IsLetterOrDigit(CurrentCharacter) Then
ResultString += CurrentCharacter
End If
Next
Return ResultString
End Function
Public Function GetPurifiedBBAN(ByVal GenericBBAN As String) As String
Dim ResultString As String = String.Empty
For Counter As Integer = 1 To GenericBBAN.Length
Dim CurrentCharacter As Char = Mid(GenericBBAN, Counter, 1)
If Char.IsLetterOrDigit(CurrentCharacter) Then
ResultString += CurrentCharacter
End If
Next
Return ResultString
End Function
Public Function GetElectronicIBAN(ByVal BBAN As String, ByVal TwoLetterISOCountryAbservation As String) As String
If BBAN.Length > 1 Then
Try
Dim ArtificialIBAN As String = GetArtificialIBAN(BBAN, TwoLetterISOCountryAbservation)
Dim SwapedIBAN As String = GetSwapedIBAN(ArtificialIBAN)
Dim NumericIBAN As String = GetNumericIBAN(SwapedIBAN)
Dim IBANCheckDigits As String = GetIBANCheckDigits(NumericIBAN)
Return UCase(TwoLetterISOCountryAbservation) & IBANCheckDigits & BBAN
Catch
Return String.Empty
End Try
Else
Return String.Empty
End If
End Function
Public Function GetPaperIBAN(ByVal GenericIBAN As String) As String
Dim PurifiedIBAN As String = GetPurifiedIBAN(GenericIBAN)
Dim ResultString As String = String.Empty
For Counter As Integer = 1 To PurifiedIBAN.Length
Dim CurrentCharacter As Char = Mid(PurifiedIBAN, Counter, 1)
ResultString += CurrentCharacter
If Counter Mod 4 = 0 Then
ResultString += " "
End If
Next
Return ResultString
End Function
Private Function GetNumericIBAN(ByVal SwapedIBAN As String) As String
Dim ResultString As String = String.Empty
For Counter As Integer = 1 To SwapedIBAN.Length
Dim CurrentCharacter As Char = UCase(Mid(SwapedIBAN, Counter, 1))
If Char.IsLetter(CurrentCharacter) Then
Select Case CurrentCharacter
Case "A"
ResultString += "10"
Case "B"
ResultString += "11"
Case "C"
ResultString += "12"
Case "D"
ResultString += "13"
Case "E"
ResultString += "14"
Case "F"
ResultString += "15"
Case "G"
ResultString += "16"
Case "H"
ResultString += "17"
Case "I"
ResultString += "18"
Case "J"
ResultString += "19"
Case "K"
ResultString += "20"
Case "M"
ResultString += "21"
Case "N"
ResultString += "22"
Case "L"
ResultString += "23"
Case "O"
ResultString += "24"
Case "P"
ResultString += "25"
Case "Q"
ResultString += "26"
Case "R"
ResultString += "27"
Case "S"
ResultString += "28"
Case "T"
ResultString += "29"
Case "U"
ResultString += "30"
Case "V"
ResultString += "31"
Case "W"
ResultString += "32"
Case "X"
ResultString += "33"
Case "Y"
ResultString += "34"
Case "Z"
ResultString += "35"
End Select
Else
ResultString += CurrentCharacter
End If
Next
Return ResultString
End Function
Private Function GetSwapedIBAN(ByVal PurifiedIBAN As String) As String
Dim ResultString As String
ResultString = String.Empty
Dim FirstFourCharacters As String = Left(PurifiedIBAN, 4)
Dim OtherCharacters As String = Mid(PurifiedIBAN, 5, PurifiedIBAN.Length - 4)
ResultString = OtherCharacters & FirstFourCharacters
Return ResultString
End Function
Private Function GetArtificialIBAN(ByVal BBAN As String, ByVal TwoLetterISOCountryAbservation As String) As String
If TwoLetterISOCountryAbservation.Length = 2 Then
Return UCase(TwoLetterISOCountryAbservation) & "00" & GetPurifiedBBAN(BBAN)
Else
Return String.Empty
End If
End Function
Private Function Mod9710(ByVal NumericIBAN As String) As Double
Return Double.Parse(NumericIBAN) Mod 97
End Function
Private Function GetIBANCheckDigits(ByVal NumericIBAN As String) As String
Dim CheckDigits As Double = 98 - Mod9710(NumericIBAN)
If CheckDigits < 10 Then
Return "0" & CheckDigits.ToString
Else
Return CheckDigits.ToString
End If
End Function
End Module
End Namespace