Archive

Posts Tagged ‘iban’

Validity check function for IBAN account

December 13th, 2007 No comments

The International Bank Account Number (IBAN) is an international standard for identifying bank accounts across national borders.
The IBAN was developed to facilitate payments within the European Union. Customers, especially individuals and small and medium enterprises, are frequently confused by differing national standards for bank account numbers.

The IBAN consists of a country code, followed by two check digits, and up to thirty alphanumeric characters for the domestic bank account number, called the BBAN (Basic Bank Account Number). It is up to each country’s national banking community to decide on the length of the BBAN for accounts in that country, but its length must be fixed for any given country.

The IBAN must not contain spaces when stored electronically. When printed on paper, however, the norm is to express it in groups of four characters, the last group being of variable length.

Since there is no native validator made for this, I’ve reproduced the algorithm so I can validate my IBAN fields.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
private function checkIBAN (returnCTRLCode:Boolean, TI:TextInput):Number{ //the function returns the control code or the validation result
 
//the TI:TextInput contains the IBAN code which must be validated
 
var tmp:String = '';
 
var tmp2:String = ''; //first operation is to extract a part of the IBAN code, which later will be transformed
 
if (returnCTRLCode) tmp = TI.text.substr(4,TI.text.length)+TI.text.substr(0,2)+'00';
else tmp = TI.text.substr(4,TI.text.length)+TI.text.substr(0,4);
//converting letters to numbers: A=10, B=11... Z=35
for (var i:int=0; i<tmp.length;i++){
if (tmp.charCodeAt(i)>64){
var c:Number =tmp.charCodeAt(i)-55;
tmp2+=c.toString();
}else{
tmp2+=tmp.charAt(i);
}
}
var counter:Number = 0; //now we have to divide the huge number above to 97. The rest represents validation result
var piece:String = tmp2.charAt(counter);
var rest:Number = 0;  //since dividing huge numbers is not possible due to Number class representation, we have to use division algorithm learned in the first years of school
while (counter<tmp2.length){
if (Number(piece)>97){ //the chunk is larger than the divider, we can divide
rest = Number(piece) % 97;
piece = rest.toString();//the piece will keep the rest so we can divide again;
}else{ //the chunk is smaller than 97, we cannot divide. We'll add another piece to the chunk
counter++;
piece+=tmp2.charAt(counter);
}
}
if (returnCTRLCode) rest=98-rest;//if needed, we can return the  control code , which is the 2 digit number  after country code
return rest;//returns the rest. If the rest is equal with 1, the IBAN account is valid
}

Mainly, we’ll send to the function which TextInput contains the IBAN account and if we want a standard validation (the number returned by the function must be equal with 1, meaning that IBAN is valid) or we want the control code of the IBAN account indicated, so we can alert the user about the error.

Categories: Adobe Flex Tags: , ,