Controllo dell’Iban
In questo articolo vedremo come verificare se un codice Iban è corretto.
E’ sempre bello dare al cliente un programmino personalizzato con il proprio nome o proprio logo, per evitare che poi lui vada a cercare su internet il controllo (e ci sono siti che lo fanno).
Il codice di verifica è molto semplice, vengono usate le espressioni regolari per verificare il formato mentre per la verifica del codice CIN viene usato il MOD 97, ossia la divisione per 97 e avere il resto.
Questo è il codice della funzione di verifica.
Public Function VALIDATEIBAN(ByVal IBAN As String) As String
Dim objRegExp As Object
Dim IBANformat As Boolean
Dim IBANNR As String
Dim ReplaceChr As String
Dim ReplaceBy As String
'Check format
IBAN = UCase(IBAN)
IBAN = Replace(IBAN, " ", "")
objRegExp = CreateObject("vbscript.regexp")
objRegExp.IgnoreCase = True
objRegExp.Global = True
objRegExp.Pattern = "[a-zA-Z]{2}[0-9]{2}[a-zA-Z0-9]{4}[0-9]{7}([a-zA-Z0-9]?){0,16}"
IBANformat = objRegExp.Test(IBAN)
'Validity of country code will not be checked!
If IBANformat = False Then
VALIDATEIBAN = "FORMAT NOT RECOGNIZED"
Else
'Flip first 4 characters to the back
IBANNR = Right(IBAN, Len(IBAN) - 4) & Left(IBAN, 4)
'Replace letters by the right numbers
For Nr = 10 To 35
ReplaceChr = Chr(Nr + 55)
ReplaceBy = Trim(Str(Nr))
IBANNR = Replace(IBANNR, ReplaceChr, ReplaceBy)
Next Nr
'Loop through the IBAN, as it is too long to calculate at one go
CurrPart = ""
Answer = ""
For CurrDigit = 1 To Len(IBANNR)
CurrPart = CurrPart & Mid$(IBANNR, CurrDigit, 1)
CurrNumber = CLng(CurrPart)
'If the number can be divided
If 97 <= CurrNumber Then
LeftOver = CurrNumber Mod 97
WorkValue = (CurrNumber - LeftOver) / 97
Answer = Answer & CStr(WorkValue)
CurrPart = CStr(LeftOver)
Else
'If no division occurred, add a trailing zero
If Len(Answer) > 0 Then
Answer = Answer & "0"
'Exception for the last number
If CurrDigit = Len(IBANNR) Then
LeftOver = CurrNumber Mod 97
Else
End If
Else
End If
End If
Next
If LeftOver = 1 Then
VALIDATEIBAN = "IBAN OK"
Else
VALIDATEIBAN = "IBAN NON OK"
End If
End If
Return VALIDATEIBAN
Exit Function
End Function
Private Function Left(iBAN As String, v As Integer) As String
Return Mid(iBAN, 1, v)
End Function
Private Function Right(iBAN As String, v As Integer) As String
Return Mid(iBAN, Len(iBAN) - v + 1, v)
End Function
Il codice non è mio, l’ho trovato su internet e poi ho fatto qualche personalizzazione.
Per lanciare la verifica, ho creato un pulsante e ci mettete questo codice:
Dim ibanStr, result As String
Dim i As Integer
Dim rr As Boolean
ibanStr = "IT03J0100501628000000000547"
result = VALIDATEIBAN(ibanStr)
If (result = "IBAN OK") Then
MsgBox("IBAN OK")
Else
MsgBox("IBAN NON OK")
End If
Ovviamente il codice Iban da verificare lo potete passare tramite una TextBox, o dati presi da un database, io per emplicità l’ho messo nel codice direttamente.
Fonte e crediti:
' Created by : Koen Rijnsent (www.castoro.nl)
' Inspired by : Chris Fannin (AbbydonKrafts)
' Inspired by : bonsvr (http://stackoverflow.com/users/872583/bonsvr)

Leggi l'articolo intero | Make a Comment ( None so far )