IBAN checker (code)

Share business templates, ideas, experiences, etc with fellow Servoy developers here

IBAN checker (code)

Postby Ron » Thu Aug 28, 2008 3:20 pm

Hi all,

I thought it would be nice to be able to check International
Bank Account Numbers (IBAN’s) in Servoy.
Lots of IBAN checkers can be found on the web but despite
this I could not get my hands on the source code.
So I made my own IBAN checker.
The background of the IBAN field will color red if it not passes the validation.
Tooltiptext will inform you what is wrong with the number.
As far as I can tell the code has no bugs, however one never knows.
Please let me know if you find one (or more).
This code is to be used at your own risk.

Paste the code in your editor.
Make an IBAN textfield and give it an elementname.
Optional: add a tooltiptextfield and give IBAN textfield a tooltip property.
Change in your code: myibanfield , myelementname (and mytooltiptextfield).

Special thanks to Vostinar Laurian for his modulo tip!
Have fun and feel free to change and extend the code the way you like.

Ron

Code: Select all


function IBANchecker()
{
//This function checks the validation of International
//Bank Account Numbers (IBAN's).
//More info: http://www.europebanks.info/ibanguide.htm
//Example valid IBAN: CY17 0020 0128 0000 0012 0052 7600
//Example valid IBAN: BE28 7350 0052 7020   
//Author: Ronald Tadema Damman
//Version: 1.00
//August, 29th, 2008
//Thanks to Vostinar Laurian for his modulo-tip!
//Replace: myibanfield, myelementname and (optional) mytooltiptextfield
//Trigger: Button or Event: OnDataChange
//USE AT YOUR OWN RISK
//
//>>>>>>> replace myibanfield  <<<<<<<<
var iban = myibanfield
var tooltiptext = ''
//
//colorsettings IBAN field
var black = "#000000"
var white = "#FFFFFF"
var red   = "#FF0000"
//
//reset color to default if field is NULL or empty and stop
if (!iban)
{
//>>>>>> replace myelementname <<<<<<<<<<
elements.myelementname.bgcolor = white;     
elements.myelementname.fgcolor = black
//
tooltiptext = "Put your IBAN here please."
mytooltiptextfield = tooltiptext
return;
}
//
//trim all spaces
var trimmed = utils.stringReplace(iban,' ', '')
//length trimmed
var trimmedlength = trimmed.length
//usual format with spaces every 4 digits
var formatted = ''
for ( var i = 1 ; i <= trimmedlength ; i++ )
{
formatted += utils.stringMiddle(trimmed,i,1)
//IBAN strings have 15 - 28 digits
if (i == 4 || i == 8 || i == 12 || i == 16 || i == 20 || i == 24 || i == 28 )
{
formatted += ' '
}
}
//countrycode first two digits
var countrycode = utils.stringLeft(trimmed,2)
//
//put first 4 digits to end of string
var rearranged = utils.stringMiddle(trimmed,5, trimmedlength - 4)+
                 utils.stringLeft(trimmed,4)
//convert alphabet characters into numbers
var converted = ''
converted = utils.stringReplace(rearranged,'A','10')
converted = utils.stringReplace(converted,'B','11')
converted = utils.stringReplace(converted,'C','12')
converted = utils.stringReplace(converted,'D','13')
converted = utils.stringReplace(converted,'E','14')
converted = utils.stringReplace(converted,'F','15')
converted = utils.stringReplace(converted,'G','16')
converted = utils.stringReplace(converted,'H','17')
converted = utils.stringReplace(converted,'I','18')
converted = utils.stringReplace(converted,'J','19')
converted = utils.stringReplace(converted,'K','20')
converted = utils.stringReplace(converted,'L','21')
converted = utils.stringReplace(converted,'M','22')
converted = utils.stringReplace(converted,'N','23')
converted = utils.stringReplace(converted,'O','24')
converted = utils.stringReplace(converted,'P','25')
converted = utils.stringReplace(converted,'Q','26')
converted = utils.stringReplace(converted,'R','27')
converted = utils.stringReplace(converted,'S','28')
converted = utils.stringReplace(converted,'T','29')
converted = utils.stringReplace(converted,'U','30')
converted = utils.stringReplace(converted,'V','31')
converted = utils.stringReplace(converted,'W','32')
converted = utils.stringReplace(converted,'X','33')
converted = utils.stringReplace(converted,'Y','34')
converted = utils.stringReplace(converted,'Z','35')
//
//split string in two parts of max 15 digits
var lengthconverted = converted.length
var multiplier = "1"
var lengthfirstpart = 15
var lengthsecondpart = lengthconverted - 15
var firstpart = utils.stringLeft(converted, 15)
var secondpart = utils.stringMiddle(converted, 16, lengthsecondpart)
//built multiplier
var multiplier = "1"
for ( var i = 1 ; i < lengthsecondpart + 1 ; i++ )
{
multiplier += "0"
}   
//modulo 97 calculation
var firstremainder = firstpart  % 97
var multiplierremainder = multiplier % 97
var secondremainder = secondpart % 97
var remainder = (firstremainder * multiplierremainder + secondremainder) % 97
//
//the remainder must be 1
if (remainder == 1 && lengthconverted < 31 && lengthconverted > 16)
{
//TRUE: IBAN (could) exist /////////////////////////////
//>>>>>> replace myelementname <<<<<<<<<<
elements.myelementname.bgcolor = white     
elements.myelementname.fgcolor = black
//
//>>>>>>>> replace myibanfield <<<<<<<<
//myibanfield is formatted
myibanfield = formatted
//information in tooltip
mytooltiptextfield = "IBAN (could) exist."
}
else
//FALSE: IBAN does not exist  //////////////////////////
{
//>>>>>> replace myelementname <<<<<<<<<<
elements.myelementname.bgcolor = red
elements.myelementname.fgcolor = white
//
var countrycode = utils.stringLeft(trimmed,2)
var countrytablelength =''
// countrycode table : 48 countries
switch(countrycode)
{
case  "AD" : countrytablelength = 24 ; break; //Andorra
case  "AT" : countrytablelength = 24 ; break; //Austria
case  "BE" : countrytablelength = 16 ; break; //Belgium
case  "BA" : countrytablelength = 20 ; break; //Bosnia and Herzegovina
case  "BG" : countrytablelength = 22 ; break; //Bulgaria
case  "HR" : countrytablelength = 21 ; break; //Croatia
case  "CH" : countrytablelength = 21 ; break; //Switserland
case  "CY" : countrytablelength = 28 ; break; //Cyprus
case  "CZ" : countrytablelength = 24 ; break; //Czech Republic
case  "DK" : countrytablelength = 18 ; break; //Denmark
case  "EE" : countrytablelength = 20 ; break; //Estonia
case  "ES" : countrytablelength = 24 ; break; //Spain
case  "FI" : countrytablelength = 18 ; break; //Finland
case  "GB" : countrytablelength = 22 ; break; //United Kingdom
case  "GL" : countrytablelength = 18 ; break; //Greenland
case  "FO" : countrytablelength = 18 ; break; //Faroe Islands
case  "FR" : countrytablelength = 27 ; break; //France
case  "DE" : countrytablelength = 22 ; break; //Germany
case  "GI" : countrytablelength = 23 ; break; //Gibraltar
case  "GR" : countrytablelength = 27 ; break; //Greece
case  "HU" : countrytablelength = 28 ; break; //Hungary
case  "IS" : countrytablelength = 26 ; break; //Iceland
case  "IE" : countrytablelength = 22 ; break; //Replublic of Ireland
case  "IL" : countrytablelength = 23 ; break; //Israel
case  "IT" : countrytablelength = 27 ; break; //Italy
case  "LV" : countrytablelength = 21 ; break; //Latvia
case  "LI" : countrytablelength = 21 ; break; //Liechtenstein
case  "LT" : countrytablelength = 20 ; break; //Lithuania
case  "LU" : countrytablelength = 20 ; break; //Luxembourg
case  "MK" : countrytablelength = 19 ; break; //Republic of Macedonia
case  "MT" : countrytablelength = 31 ; break; //Malta
case  "MC" : countrytablelength = 27 ; break; //Monaco
case  "ME" : countrytablelength = 22 ; break; //Montenegro
case  "MA" : countrytablelength = 24 ; break; //Morocco
case  "MU" : countrytablelength = 30 ; break; //Mauritius
case  "MK" : countrytablelength = 19 ; break; //Macedonia
case  "NL" : countrytablelength = 18 ; break; //Netherlands
case  "NO" : countrytablelength = 15 ; break; //Norway
case  "PL" : countrytablelength = 28 ; break; //Poland
case  "PT" : countrytablelength = 25 ; break; //Portugal
case  "RO" : countrytablelength = 24 ; break; //Romania
case  "SM" : countrytablelength = 27 ; break; //San Marino
case  "RS" : countrytablelength = 22 ; break; //Servia
case  "SK" : countrytablelength = 24 ; break; //Slovakia
case  "SI" : countrytablelength = 19 ; break; //Slovenia
case  "SE" : countrytablelength = 24 ; break; //Sweden
case  "TR" : countrytablelength = 26 ; break; //Turkey
case  "TN" : countrytablelength = 24 ; break; //Tunesia
default    : countrytablelength = "nonexistent"     
}
//Error messages (extend and change if you like)
if (countrytablelength == "nonexistent")
        {tooltiptext = "\"" + countrycode + "\"" + " is a nonexisting countrycode."}
else if (countrytablelength == trimmedlength)
        {tooltiptext = "StringLength is OK, number is incorrect."}
else if (trimmedlength > countrytablelength)
        {tooltiptext = "Stringlength: " + trimmedlength + " is too large, it should be: " +
        countrytablelength}
else if (trimmedlength < countrytablelength)
        {tooltiptext = "Stringlength: " + trimmedlength + " is too small, it should be: " +
       countrytablelength}
//
//output check info in tooltip or field
//>>>>>>> replace mytooltiptextfield <<<<<<<<<
mytooltiptextfield = tooltiptext
//
}


Last edited by Ron on Fri Aug 29, 2008 12:15 pm, edited 1 time in total.
Ron
 
Posts: 315
Joined: Fri May 23, 2003 12:30 am
Location: Netherlands

Re: IBAN checker (code)

Postby tclavering » Fri Aug 29, 2008 11:23 am

Hi Ron,
nice code sample but I don't think your country code list is up to date.

I found this handy free IBAN validator web service the other day:
https://www.unifiedsoftware.co.uk/com/iban.php
wsdl - http://www.unifiedsoftware.co.uk/freeibanvalidate.wsdl

Cheers
Tony
tclavering
 
Posts: 1
Joined: Fri Aug 29, 2008 10:37 am

Re: IBAN checker (code)

Postby Ron » Fri Aug 29, 2008 12:22 pm

Hi Tony,

Thanks for your reply.
The missing countries were Greenland, Mauritius and Macedonia.
I updated the code.

Regards,
Ron
Ron
 
Posts: 315
Joined: Fri May 23, 2003 12:30 am
Location: Netherlands


Return to Sharing Central

Who is online

Users browsing this forum: No registered users and 1 guest

cron