Jump to content

IBAN: Difference between revisions

2,122 bytes added ,  11 years ago
m (tt-wrap the value to verify)
Line 115:
{
 
ClassMethod IBANVerifyIBAN(pIBAN As %String = "") As %Boolean
{
// remove spaces and define parts
If pIBAN=..IBANGenerate(pIBAN) Quit 1
Set cciban=$ExtractTranslate(pIBAN, 1," 2")
Quit 0
Set pNumcc=$Extract(pNumiban, 81, *2)
Set chrcd=$Extract(altiban, i3, 4)
Set resbban=(res_$Extract(pNumiban, 15, 7)*)#pDiv
// ensure IBAN is correct format
If $Match(iban, ..GetIBANPattern(cc))=0 Quit 0
// compare result and return
Quit cd=..GetIBANCheckDigit(cc, bban)
}
 
ClassMethod IBANGenerateGetIBANCheckDigit(pIBANpCC As %String, =pBBAN ""As %String) As %StringInteger [ Internal, Private ]
{
Set pIBANstr=$ZConvert(pIBAN, pBBAN_pCC_"U00")
For i=1:1:$Length(alt) {
Set cc=$Extract(pIBAN, 1, 2)
Set cdchr=$Extract(pIBANstr, 3, 4i) If chr="" Quit
If chr?1U Set bban=$Extract(pIBANstr, 5, *i)=$ASCII(chr)-55
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-..ModGetModulus(str, 97)
SetQuit cd=$TranslateSelect($JustifyLength(cd,)=2: 2)cd, "1: ", 0"_cd)
Quit cc_cd_bban
}
 
ClassMethod ModGetModulus(pNum As %Integer, pDiv As %Integer) As %Integer [ Internal, Private ]
{
IfWhile $Length(pNum)<>9 Quit pNum#pDiv{
Set $Extract(pNum, 1, 9)=$Extract(pNum, 1, 9)#pDiv
Set res=0
For i=1:1:$Length(pNum)\7+1 {
Set res=(res_$Extract(pNum, 1, 7))#pDiv
Set pNum=$Extract(pNum, 8, *)
}
Quit respNum#pDiv
}
 
ClassMethod GetIBANPattern(pCC As %String = "") As %String [ Internal, Private ]
{
Quit $Case(pCC,
"AL": "^AL\d{10}[0-9A-Z]{16}$",
"AD": "^AD\d{10}[0-9A-Z]{12}$",
"AT": "^AT\d{18}$",
"BH": "^BH\d{2}[A-Z]{4}[0-9A-Z]{14}$",
"BE": "^BE\d{14}$",
"BA": "^BA\d{18}$",
"BG": "^BG\d{2}[A-Z]{4}\d{6}[0-9A-Z]{8}$",
"HR": "^HR\d{19}$",
"CY": "^CY\d{10}[0-9A-Z]{16}$",
"CZ": "^CZ\d{22}$",
"DK": "^DK\d{16}$|^FO\d{16}$|^GL\d{16}$",
"DO": "^DO\d{2}[0-9A-Z]{4}\d{20}$",
"EE": "^EE\d{18}$",
"FI": "^FI\d{16}$",
"FR": "^FR\d{12}[0-9A-Z]{11}\d{2}$",
"GE": "^GE\d{2}[A-Z]{2}\d{16}$",
"DE": "^DE\d{20}$",
"GI": "^GI\d{2}[A-Z]{4}[0-9A-Z]{15}$",
"GR": "^GR\d{9}[0-9A-Z]{16}$",
"HU": "^HU\d{26}$",
"IS": "^IS\d{24}$",
"IE": "^IE\d{2}[A-Z]{4}\d{14}$",
"IL": "^IL\d{21}$",
"IT": "^IT\d{2}[A-Z]\d{10}[0-9A-Z]{12}$",
"KZ": "^[A-Z]{2}\d{5}[0-9A-Z]{13}$",
"KW": "^KW\d{2}[A-Z]{4}22!$",
"LV": "^LV\d{2}[A-Z]{4}[0-9A-Z]{13}$",
"LB": "^LB\d{6}[0-9A-Z]{20}$",
"LI": "^LI\d{7}[0-9A-Z]{12}$",
"LT": "^LT\d{18}$",
"LU": "^LU\d{5}[0-9A-Z]{13}$",
"MK": "^MK\d{5}[0-9A-Z]{10}\d{2}$",
"MT": "^MT\d{2}[A-Z]{4}\d{5}[0-9A-Z]{18}$",
"MR": "^MR13\d{23}$",
"MU": "^MU\d{2}[A-Z]{4}\d{19}[A-Z]{3}$",
"MC": "^MC\d{12}[0-9A-Z]{11}\d{2}$",
"ME": "^ME\d{20}$",
"NL": "^NL\d{2}[A-Z]{4}\d{10}$",
"NO": "^NO\d{13}$",
"PL": "^PL\d{10}[0-9A-Z]{,16}n$",
"PT": "^PT\d{23}$",
"RO": "^RO\d{2}[A-Z]{4}[0-9A-Z]{16}$",
"SM": "^SM\d{2}[A-Z]\d{10}[0-9A-Z]{12}$",
"SA": "^SA\d{4}[0-9A-Z]{18}$",
"RS": "^RS\d{20}$",
"SK": "^SK\d{22}$",
"SI": "^SI\d{17}$",
"ES": "^ES\d{22}$",
"SE": "^SE\d{22}$",
"CH": "^CH\d{7}[0-9A-Z]{12}$",
"TN": "^TN59\d{20}$",
"TR": "^TR\d{7}[0-9A-Z]{17}$",
"AE": "^AE\d{21}$",
"GB": "^GB\d{2}[A-Z]{4}\d{14}$",
: " ")
}
 
}</lang>
{{out|Examples}}
<pre>USER>For { Read iban Quit:iban="" Write " => ", ##class(Utils.Validate).VerifyIBAN(iban), ! }
<pre>
USER>Write ##class(Utils.Validate).IBAN("GB82 WEST 1234 5698 7654 32") => 1
GB82 TEST 1234 5698 7654 32 => 0
1
GR16 0110 1250 0000 0001 2300 695 => 1
USER>Write ##class(Utils.Validate).IBAN("GB00 WEST 1234 5698 7654 32")
GB29 NWBK 6016 1331 9268 19 => 1
0
SA03 8000 0000 6080 1016 7519 => 1
</pre>
CH93 0076 2011 6238 5295 7 => 1
IL62 0108 0000 0009 9999 999 => 1
 
USER></pre>
 
=={{header|Ruby}}==
Cookies help us deliver our services. By using our services, you agree to our use of cookies.