IBAN: Difference between revisions

From Rosetta Code
Content added Content deleted
(added Caché ObjectScript)
 
m (Minor edit)
Line 1: Line 1:
{{draft task}}
{{draft task}}
{{wikipedia}}
{{wikipedia}}
The [[wp:International_Bank_Account_Number|International Bank Account Number (IBAN)]] is an internationally agreed means of identifying bank accounts across national borders with a reduced risk of propagating [[wp:Transcription_error|transcription errors]. The IBAN consists of up to 34 alphanumeric characters: first the two-letter ISO 3166-1 alpha-2 country code, then two check digits, and finally a country-specific Basic Bank Account Number (BBAN). The check digits enable a sanity check of the bank account number to confirm its integrity even before submitting a transaction.
The [[wp:International_Bank_Account_Number|International Bank Account Number (IBAN)]] is an internationally agreed means of identifying bank accounts across national borders with a reduced risk of propagating [[wp:Transcription_error|transcription errors]]. The IBAN consists of up to 34 alphanumeric characters: first the two-letter ISO 3166-1 alpha-2 country code, then two check digits, and finally a country-specific Basic Bank Account Number (BBAN). The check digits enable a sanity check of the bank account number to confirm its integrity even before submitting a transaction.


The task here is to validate the following fictitious IBAN: GB82 WEST 1234 5698 7654 32. Details of the algorithm can be found on the Wikipedia page.
The task here is to validate the following fictitious IBAN: GB82 WEST 1234 5698 7654 32. Details of the algorithm can be found on the Wikipedia page.
Line 10: Line 10:
{
{


ClassMethod IBAN(iban As %String = "") As %Boolean
ClassMethod IBAN(pIBAN As %String = "") As %Boolean
{
{
If iban=..IBANGenerate(iban) Quit 1
If pIBAN=..IBANGenerate(pIBAN) Quit 1
Quit 0
Quit 0
}
}


ClassMethod IBANGenerate(iban As %String = "") As %String
ClassMethod IBANGenerate(pIBAN As %String = "") As %String
{
{
Set iban=$ZConvert(iban, "U")
Set pIBAN=$ZConvert(pIBAN, "U")
Set cc=$Extract(iban, 1, 2)
Set cc=$Extract(pIBAN, 1, 2)
Set cd=$Extract(iban, 3, 4)
Set cd=$Extract(pIBAN, 3, 4)
Set bban=$Extract(iban, 5, *)
Set bban=$Extract(pIBAN, 5, *)
Set str="", alt=$ZStrip(bban_cc_"00", "*E'U'N")
Set str="", alt=$ZStrip(bban_cc_"00", "*E'U'N")
For i=1:1:$Length(alt) {
For i=1:1:$Length(alt) {
Line 33: Line 33:
}
}


ClassMethod Mod(str As %String, div As %Integer) As %Integer [ Internal, Private ]
ClassMethod Mod(num As %Integer, div As %Integer) As %Integer [ Internal, Private ]
{
{
If $Length(str)<9 Quit str#div
If $Length(num)<9 Quit num#div
Set res=0
Set res=0
For i=1:1:$Length(str)\7+1 {
For i=1:1:$Length(num)\7+1 {
Set res=(res_$Extract(str, 1, 7))#div
Set res=(res_$Extract(num, 1, 7))#div
Set str=$Extract(str, 8, *)
Set num=$Extract(num, 8, *)
}
}
Quit res
Quit res

Revision as of 12:45, 31 March 2013

IBAN is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.
This page uses content from Wikipedia. The original article was at IBAN. The list of authors can be seen in the page history. As with Rosetta Code, the text of Wikipedia is available under the GNU FDL. (See links for details on variance)

The International Bank Account Number (IBAN) is an internationally agreed means of identifying bank accounts across national borders with a reduced risk of propagating transcription errors. The IBAN consists of up to 34 alphanumeric characters: first the two-letter ISO 3166-1 alpha-2 country code, then two check digits, and finally a country-specific Basic Bank Account Number (BBAN). The check digits enable a sanity check of the bank account number to confirm its integrity even before submitting a transaction.

The task here is to validate the following fictitious IBAN: GB82 WEST 1234 5698 7654 32. Details of the algorithm can be found on the Wikipedia page.

Caché ObjectScript

<lang cache>Class Utils.Validate [ Abstract ] {

ClassMethod IBAN(pIBAN As %String = "") As %Boolean { If pIBAN=..IBANGenerate(pIBAN) Quit 1 Quit 0 }

ClassMethod IBANGenerate(pIBAN As %String = "") As %String { Set pIBAN=$ZConvert(pIBAN, "U") Set cc=$Extract(pIBAN, 1, 2) Set cd=$Extract(pIBAN, 3, 4) Set bban=$Extract(pIBAN, 5, *) Set str="", alt=$ZStrip(bban_cc_"00", "*E'U'N") For i=1:1:$Length(alt) { Set chr=$Extract(alt, i) If chr?1U Set chr=$ASCII(chr)-55 Set str=str_chr } Set cd=98-..Mod(str, 97) Set cd=$Translate($Justify(cd, 2), " ", 0) Quit cc_cd_bban }

ClassMethod Mod(num As %Integer, div As %Integer) As %Integer [ Internal, Private ] { If $Length(num)<9 Quit num#div Set res=0 For i=1:1:$Length(num)\7+1 { Set res=(res_$Extract(num, 1, 7))#div Set num=$Extract(num, 8, *) } Quit res }

}</lang>

Examples:
USER>Write ##class(Utils.Validate).IBAN("GB82 WEST 1234 5698 7654 32")
1
USER>Write ##class(Utils.Validate).IBAN("GB00 WEST 1234 5698 7654 32")
0