Teste si le N° IBAN est valide

Teste si le N° IBAN est valide

Messagepar Michael DELIQUE » 25 Oct 2007 à 11:57

Code : Tout sélectionner
Function IsValideIBAN(wIBAN As String )As Integer
   
   'Déclaration Variables
   Dim   lstValue List As String
   Dim IBAN As String
   Dim IBAN2 As String
   Dim Char As String
   Dim i As Integer
   
   On Error Goto ErreurHandle
   
   If Trim(wIBAN) = "" Then
      IsValideIBAN = False
      Exit Function
   End If
   
   'retire les caracteres exédentaires
   IBAN = ""
   For i = 1 To Len(wIBAN)
      Char = Mid(wIBAN,i,1)
      If Char Like "[a-z,A-Z,0-9]"  Then
         IBAN = IBAN + Trim(Char)
      End If
      Char = ""
   Next
   
   'vérifie la longeur maximal du code IBAN
   If Len(IBAN)>34 Then
      IBAN = ""
      IsValideIBAN = False
      Exit Function   
   End If
   
   If Ucase(Left(IBAN,4)) = "IBAN" Then
      IBAN = Right(IBAN,Len(IBAN)-4)
   End If
   
   IBAN = Right(IBAN,Len(IBAN)-4)+Left(IBAN,4)
   
   lstValue("A") = "10"
   lstValue("B") = "11"
   lstValue("C") = "12"
   lstValue("D") = "13"
   lstValue("E") = "14"
   lstValue("F") = "15"
   lstValue("G") = "16"
   lstValue("H") = "17"
   lstValue("I") = "18"
   lstValue("J") = "19"
   lstValue("K") = "20"
   lstValue("L") = "21"
   lstValue("M") = "22"
   lstValue("N") = "23"
   lstValue("O") = "24"
   lstValue("P") = "25"
   lstValue("Q") = "26"
   lstValue("R") = "27"
   lstValue("S") = "28"
   lstValue("T") = "29"
   lstValue("U") = "30"
   lstValue("V") = "31"
   lstValue("W") = "32"
   lstValue("X") = "33"
   lstValue("Y") = "34"
   lstValue("Z") = "35"   
   lstValue("0") = "0"
   lstValue("1") = "1"
   lstValue("2") = "2"
   lstValue("3") = "3"
   lstValue("4") = "4"
   lstValue("5") = "5"
   lstValue("6") = "6"
   lstValue("7") = "7"
   lstValue("8") = "8"
   lstValue("9") = "9"
   
   'conversion des lettres en chiffres
   For i = 1 To Len(Trim(IBAN))
      Char = Mid(IBAN,i,1)
      If Trim(Char) <> "" Then
         IBAN2 = IBAN2 + lstValue(Ucase(Char))
      End If
      Char = ""
   Next
   Erase lstValue
   IBAN = ""
   
   If ModuloBigNumber(IBAN2,97) = 1 Then
      IsValideIBAN = True
   Else
      IsValideIBAN = False
   End If
   
   IBAN2 = ""
   
%REM

Wikipedia

Vérification
Enlever les caractères indésirables (espaces, tirets)
Déplacer les 4 premiers caractères à droite
Convertir les lettres en chiffres via une table de conversion (A=10, B=11, C=12 etc)
Diviser le nombre ainsi obtenu par 97. Si le reste est égal à 1 l'IBAN est correct : Modulo de 97 égal à 1

Exemples
BE43 0689 9999 9501

BE43068999999501
068999999501BE43
068999999501111443
068999999501111443 Modulo 97 = 1
NB : les comptes français numériques ont tous la même clé IBAN égale à 76.
Cela est dû à la clé du RIB français qui est elle même un modulo 97.
La correspondance numérique des lettres étant différente entre le calcul de la clé RIB et le calcul de la clé IBAN,
cela n'est pas vrai pour les comptes français qui comportent au moins une lettre.

%END REM
   Exit Function
ErreurHandle:
   Msgbox "("+Cstr(Getthreadinfo (1))+" Call by "+Cstr(Getthreadinfo(10))+")"+Chr(10)+"Erreur " + Str(Err) + " : "+Chr(10) + Cstr(Error)+". "+Chr(10)+"Ligne N° "+Cstr(Erl),16," ERREUR !"
   IsValideIBAN = False
   Exit Function   
End Function



Code : Tout sélectionner
Function ModuloBigNumber(wNumber As String, wnbDiviseur As Long) As Integer
   
   'cette fonction permet de faire un modulo sur un tres grand nombre entier  ne pouvant pas être géré par la functon Mod
   Dim LeftNumber As String
   Dim RightNumber As String
   Dim i As Integer
   
   On Error Goto ErreurHandle
   
   If Trim(wNumber) = "" Then
      ModuloBigNumber = -1
      Exit Function
   End If
   
   'vérifie qu'il n'y que des chiffres
   If wNumber Like "*[!0-9]*"  Then
      ModuloBigNumber = -1
      Exit Function
   End If
   
   i = Len(Cstr(wnbDiviseur))
   LeftNumber = Left(wNumber, i)
   RightNumber = Mid(wNumber, i+1)
   
   Do
      LeftNumber = Cstr(Clng(LeftNumber) Mod wnbDiviseur)
      LeftNumber= LeftNumber + Left(RightNumber, 1)
      RightNumber= Mid(RightNumber, 2)
   Loop While RightNumber<>""
   
   ModuloBigNumber = Clng(LeftNumber) Mod wnbDiviseur
   
   RightNumber = ""
   LeftNumber = ""
   
   
   Exit Function
ErreurHandle:
   Msgbox "("+Cstr(Getthreadinfo (1))+" Call by "+Cstr(Getthreadinfo(10))+")"+Chr(10)+"Erreur " + Str(Err) + " : "+Chr(10) + Cstr(Error)+". "+Chr(10)+"Ligne N° "+Cstr(Erl),16," ERREUR !"
   ModuloBigNumber = -1
   Exit Function   
End Function
Cordialement

Michael (SMS-Phobique)
----------------------------
"La théorie, c'est quand on sait tout et que rien ne fonctionne. La pratique, c'est quand tout fonctionne et que personne ne sait pourquoi."
Albert EINSTEIN
Avatar de l’utilisateur
Michael DELIQUE
Administrateur
Administrateur
 
Message(s) : 12183
Inscrit(e) le : 16 Déc 2004 à 10:36
Localisation : Paris/Cergy

Retour vers Divers