IBAN: Difference between revisions
m
→{{header|Wren}}: Minor tidy
m (cleaner gawk invocation) |
m (→{{header|Wren}}: Minor tidy) |
||
(43 intermediate revisions by 24 users not shown) | |||
Line 20:
Details of the algorithm can be found on the Wikipedia page.
<br><br>
=={{header|11l}}==
{{trans|Python}}
<syntaxhighlight lang="11l">F mod97(numberstring)
V segstart = 0
V step = 9
V prepended = ‘’
V number = 0
L segstart < numberstring.len - step
number = Int(prepended‘’numberstring[segstart .< segstart + step])
V remainder = number % 97
prepended = String(remainder)
I remainder < 10
prepended = ‘0’prepended
segstart += step
step = 7
number = Int(prepended‘’numberstring[segstart ..])
R number % 97
V country2length = [‘IS’ = 26, ‘IT’ = 27, ‘MT’ = 31, ‘MU’ = 30, ‘MR’ = 27, ‘MK’ = 19, ‘IE’ = 22, ‘MD’ = 24,
‘ME’ = 22, ‘MC’ = 27, ‘IL’ = 23, ‘DE’ = 22, ‘DO’ = 28, ‘DK’ = 18, ‘PK’ = 24, ‘HR’ = 21,
‘HU’ = 28, ‘FI’ = 18, ‘LU’ = 20, ‘LT’ = 20, ‘LV’ = 21, ‘KW’ = 30, ‘LI’ = 21, ‘PS’ = 29,
‘PT’ = 25, ‘LB’ = 28, ‘GB’ = 22, ‘GE’ = 22, ‘CR’ = 21, ‘TR’ = 26, ‘GI’ = 23, ‘GL’ = 18,
‘CY’ = 28, ‘CZ’ = 24, ‘GR’ = 27, ‘GT’ = 28, ‘KZ’ = 20, ‘TN’ = 24, ‘CH’ = 21, ‘SE’ = 24,
‘SA’ = 24, ‘SM’ = 27, ‘SI’ = 19, ‘SK’ = 24, ‘PL’ = 28, ‘BR’ = 29, ‘FR’ = 27, ‘FO’ = 18,
‘BA’ = 20, ‘BG’ = 22, ‘BE’ = 16, ‘BH’ = 22, ‘VG’ = 24, ‘RO’ = 24, ‘RS’ = 22, ‘NO’ = 15,
‘NL’ = 18, ‘AZ’ = 28, ‘EE’ = 20, ‘AT’ = 20, ‘ES’ = 24, ‘AL’ = 28, ‘AD’ = 24, ‘AE’ = 23]
F valid_iban(iban_)
V iban = iban_.replace(‘ ’, ‘’).replace("\t", ‘’)
I !re:‘[\dA-Z]+’.match(iban)
R 0B
I iban.len != :country2length[iban[0.<2]]
R 0B
iban = iban[4..]‘’iban[0.<4]
R mod97(iban.map(ch -> String(Int(ch, radix' 36))).join(‘’)) == 1
L(account) [‘GB82 WEST 1234 5698 7654 32’,
‘GB82 TEST 1234 5698 7654 32’]
print(‘#. validation is: #.’.format(account, valid_iban(account)))</syntaxhighlight>
{{out}}
<pre>
GB82 WEST 1234 5698 7654 32 validation is: 1B
GB82 TEST 1234 5698 7654 32 validation is: 0B
</pre>
=={{header|Ada}}==
<
function Is_Legal(Iban : String) return Boolean;
end Iban_Code;</
with Ada.Containers.Hashed_Maps;
with Ada.Strings.Hash;
Line 111 ⟶ 160:
Nations.insert("TR", 26); Nations.insert("AE", 23);
Nations.insert("GB", 22); Nations.insert("VG", 24);
end Iban_Code;</
<
with Iban_Code;
Line 132 ⟶ 181:
Check("GB82 TEST 1234 5698 7654 32");
Check("GB82 WEST 1243 5698 7654 32");
end Check_Iban;</
GB82 WEST 1234 5698 7654 32 is valid.
GB82WEST12345698765432 is valid.
Line 138 ⟶ 187:
GB82 TEST 1234 5698 7654 32 is not valid.
GB82 WEST 1243 5698 7654 32 is not valid.
=={{header|AppleScript}}==
<syntaxhighlight lang="applescript">on countryCodes()
-- A list of 34 lists. The nth list (1-indexed) contains country codes for countries having n-character IBANS.
return {{}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {"NO"}, {"BE"}, ¬
{}, {"DK", "FO", "FI", "GL", "NL"}, {"MK", "SI"}, {"AT", "BA", "EE", "KZ", "XK", "LT", "LU"}, ¬
{"HR", "LV", "LI", "CH"}, {"BH", "BG", "CR", "GE", "DE", "IE", "ME", "RS", "GB", "VA"}, ¬
{"TL", "GI", "IQ", "IL", "AE"}, {"AD", "CZ", "MD", "PK", "RO", "SA", "SK", "ES", "SE", "TN", "VG"}, ¬
{"PT", "ST"}, {"IS", "TR"}, {"FR", "GR", "IT", "MR", "MC", "SM"}, ¬
{"AL", "AZ", "BY", "CY", "DO", "SV", "GT", "HU", "LB", "PL"}, {"BR", "EG", "PS", "QA", "UA"}, ¬
{"JO", "KW", "MU"}, {"MT", "SC"}, {"LC"}, {}, {}}
end countryCodes
on validateIBAN(iban)
-- Remove any spaces.
if (iban contains space) then set iban to replaceText(iban, space, "")
considering diacriticals but ignoring case
-- Check the length both overall and against the presumed country code.
set characterCount to (count iban)
if ((characterCount > 34) or (text 1 thru 2 of iban is not in item characterCount of countryCodes())) then return false
-- Move the first four characters to the end.
set iban to text 5 thru -1 of iban & (text 1 thru 4 of iban)
-- Replace any unadorned Latin letters with the appropriate number characters ("A" = "11" … "Z" = "35").
repeat with letter in "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
if (iban contains letter) then set iban to replaceText(iban, letter, ((letter's id) mod 32 + 9) as text)
end repeat
end considering
-- Check that what's left only contains digit characters.
if (replaceText(iban, characters of "0123456789", "") is not "") then return false
-- Calculate the mod-97 remainder.
set characterCount to (count iban)
set c to (characterCount - 1) mod 7 + 1
set mod97 to (text 1 thru c of iban) mod 97
repeat with c from (c + 1) to characterCount by 7
set mod97 to (mod97 * 10000000 + (text c thru (c + 6) of iban)) mod 97
end repeat
return (mod97 = 1)
end validateIBAN
on replaceText(txt, searchStr, replaceText)
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to searchStr
set txt to txt's text items
set AppleScript's text item delimiters to replaceText
set txt to txt as text
set AppleScript's text item delimiters to astid
return txt
end replaceText
-- Test code (IBANs borrowed from other solutions on this page.):
local testIBANs, output, thisIBAN, astid
set testIBANs to {"GB82 WEST 1234 5698 7654 32", "gb82 west 1234 5698 7654 32", "GB82 TEST 1234 5698 7654 32", ¬
"SA03 8000 0000 6080 1016 7519", "ZZ12 3456 7890 1234 5678 12", "IL62 0108 0000 0009 9999 999"}
set output to {}
repeat with thisIBAN in testIBANs
set end of output to thisIBAN & item ((validateIBAN(thisIBAN) as integer) + 1) of {": invalid", ": valid"}
end repeat
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to linefeed
set output to output as text
set AppleScript's text item delimiters to astid
return output</syntaxhighlight>
{{output}}
<syntaxhighlight lang="applescript">"GB82 WEST 1234 5698 7654 32: valid
gb82 west 1234 5698 7654 32: valid
GB82 TEST 1234 5698 7654 32: invalid
SA03 8000 0000 6080 1016 7519: valid
ZZ12 3456 7890 1234 5678 12: invalid
IL62 0108 0000 0009 9999 999: valid"</syntaxhighlight>
=={{header|Arturo}}==
<syntaxhighlight lang="rebol">ibanSize: #[
AL: 28, AD: 24, AT: 20, AZ: 28, BE: 16
BH: 22, BA: 20, BR: 29, BG: 22, CR: 21, HR: 21, CY: 28
CZ: 24, DK: 18, DO: 28, EE: 20, FO: 18, FI: 18, FR: 27
GE: 22, DE: 22, GI: 23, GR: 27, GL: 18, GT: 28, HU: 28
IS: 26, IE: 22, IL: 23, IT: 27, KZ: 20, KW: 30, LV: 21
LB: 28, LI: 21, LT: 20, LU: 20, MK: 19, MT: 31, MR: 27
MU: 30, MC: 27, MD: 24, ME: 22, NL: 18, NO: 15, PK: 24
PS: 29, PL: 28, PT: 25, RO: 24, SM: 27, SA: 24, RS: 22
SK: 24, SI: 19, ES: 24, SE: 24, CH: 21, TN: 24, TR: 26
AE: 23, GB: 22, VG: 24
]
base36: (map 0..9 'x -> to :string x) ++
map 97..122 'x -> upper to :string to :char x
valid?: function [iban][
iban: replace iban " " ""
if not? contains? iban {/[0-9A-Z]+/} -> return false
if not? (size iban)=ibanSize\[slice iban 0 1] -> return false
iban: (slice iban 4 (size iban)-1) ++ slice iban 0 3
iban: join map split iban 'ch -> to :string index base36 ch
iban: to :integer iban
return 1=iban%97
]
loop ["GB82 WEST 1234 5698 7654 32"
"GB82 TEST 1234 5698 7654 32"] 'ib
-> print [ib "=> valid?" valid? ib]</syntaxhighlight>
{{out}}
<pre>GB82 WEST 1234 5698 7654 32 => valid? true
GB82 TEST 1234 5698 7654 32 => valid? false</pre>
=={{header|AutoHotkey}}==
{{works with|AutoHotkey 1.1}}
<
, "gb82 WEST 1234 5698 7654 32"
, "GB82WEST12345698765432"
Line 183 ⟶ 344:
}
return rem
}</
GB82 WEST 1234 5698 7654 32 is valid.
gb82 WEST 1234 5698 7654 32 is valid.
Line 196 ⟶ 357:
This requires a gawk with extensions and GNU MP+MPFR support - it's usually the case. Some country codes are missing, the output is itself parsable.
<
@load "ordchr"
Line 246 ⟶ 407:
ibancsum % 97 == 1 ? valid() : invalid()
}</
Creating a test file and launching the script:
<syntaxhighlight lang="text">
cat > test.iban
FR33 ^__^ 0BAD
Line 258 ⟶ 419:
^D
gawk -Mf iban.gawk test.iban
</syntaxhighlight>
Output:
<syntaxhighlight lang="text">
INVALID FR33 ^__^ 0BAD
INVALID AA11 1234 6543 1212
Line 269 ⟶ 430:
VALID__ GB82 WEST 1234 5698 7654 32
INVALID GB82 TEST 1234 5698 7654 32
</syntaxhighlight>
=={{header|BBC BASIC}}==
{{works with|BBC BASIC for Windows}}
<
REM http://www.cnb.cz/cs/platebni_styk/iban/download/EBS204.pdf
Line 365 ⟶ 526:
num$=MID$(num$,8)
ENDWHILE
=VALmod$</
valid IBAN: GB82 WEST 1234 5698 7654 32
valid IBAN: GB82WEST12345698765432
Line 378 ⟶ 539:
invalid IBAN: US12 3456 7890 0987 6543 210 ***error!*** invalid country code: US
invalid IBAN: GR16 0110 1250 0000 0001 2300 695X ***error!*** invalid code length, expected length: 27
=={{header|Befunge}}==
<syntaxhighlight lang="befunge">>>" :NABI">:#,_>:~:"`"`48**-:55+-#v_$0::6g0>>8g-!\7g18g-!*!v>v>>
<<_#v:#78#`8#+<^+!!*-*84\-9:g8:p8\<oo>1#$-#<0$>>>#v_"dilav" ^#<<
>>^ >*-:20p9`9*1+55+**20g+"a"%10g1+00g%:4-!|^g6::_v#-*88:+1_vv>>
<<^+`"Z"\+`\"0"\*`\"A"\`"9":::\-*86:g8p01:<<40p00_v#!--+99g5<v<<
>>" si rebmun tahT">:#,_ 55+".",,@ >0"dilavni">>>
"-(&(/$$*$(*.-*.('$)*%0-&**.$'(.'/.+,''&&*.)**&,-.&.(*(*-!(%01-)
BFBBBBFFTNRRGGCCGCGGSSKSKSSDDHDHCPLPTLLLLPPAAVEIIAAAIEMMMNGMMMMI
ERGAHRIONLSOTRZYBRLIKIWMZAEOKREUHSBTRIVTUKLEDGESTLTZESEDCOEKUTRL</syntaxhighlight>
{{out}}
<pre>IBAN: GB82 WEST 1234 5698 7654 32
That number is valid.</pre>
<pre>IBAN: GB82 EAST 1234 5698 7654 32
That number is invalid.</pre>
=={{header|Bracmat}}==
<
= table country cd len N c
. (AL.28) (AD.24) (AT.20) (AZ.28) (BE.16) (BH.22) (BA.20) (BR.29)
Line 430 ⟶ 610:
& IBAN-check$"GB82 WEST 1243 5698 7654 32"
& IBAN-check$"GB82 west 1243 5698 7654 32"
);</
wrong length
invalid country code: 'GX'
Line 441 ⟶ 621:
=={{header|C}}==
<
#include <ctype.h>
#include <stdio.h>
Line 515 ⟶ 695:
/* Allocate space for the transformed IBAN. */
trans = alloca(sz + l + 1);
trans[sz + l] = 0;
Line 537 ⟶ 717:
return 0;
}</
iban 'GB82 WEST 1234 5698 7654 32' GB82TEST12345698765432
GB82WEST12345698765432 is valid.
GB82TEST12345698765432 is invalid.
=={{header|C sharp}}==
<
{
public ValidationResult Validate(string value)
Line 742 ⟶ 841:
{"VG", 24}
};
}</
<
{
[Fact]
Line 842 ⟶ 941:
Assert.Equal(ValidationResult.IsValid, result);
}
}</
=={{header|C++}}==
<syntaxhighlight lang="cpp">#include <string>
#include <iostream>
#include <boost/algorithm/string.hpp>
#include <map>
#include <algorithm>
#include <cctype>
using namespace boost::algorithm;
bool isValid(const std::string &ibanstring) {
static std::map<std::string, int> countrycodes {
{"AL", 28}, {"AD", 24}, {"AT", 20}, {"AZ", 28 },
{"BE", 16}, {"BH", 22}, {"BA", 20}, {"BR", 29 },
{"BG", 22}, {"CR", 21}, {"HR", 21}, {"CY", 28 },
{"CZ", 24}, {"DK", 18}, {"DO", 28}, {"EE", 20 },
{"FO", 18}, {"FI", 18}, {"FR", 27}, {"GE", 22 },
{"DE", 22}, {"GI", 23}, {"GR", 27}, {"GL", 18 },
{"GT", 28}, {"HU", 28}, {"IS", 26}, {"IE", 22 },
{"IL", 23}, {"IT", 27}, {"KZ", 20}, {"KW", 30 },
{"LV", 21}, {"LB", 28}, {"LI", 21}, {"LT", 20 },
{"LU", 20}, {"MK", 19}, {"MT", 31}, {"MR", 27 },
{"MU", 30}, {"MC", 27}, {"MD", 24}, {"ME", 22 },
{"NL", 18}, {"NO", 15}, {"PK", 24}, {"PS", 29 },
{"PL", 28}, {"PT", 25}, {"RO", 24}, {"SM", 27 },
{"SA", 24}, {"RS", 22}, {"SK", 24}, {"SI", 19 },
{"ES", 24}, {"SE", 24}, {"CH", 21}, {"TN", 24 },
{"TR", 26}, {"AE", 23}, {"GB", 22}, {"VG", 24 }
};
std::string teststring(ibanstring);
erase_all(teststring, " "); //defined in boost/algorithm/string.hpp
if (countrycodes.find(teststring.substr(0, 2)) == countrycodes.end())
return false;
if (teststring.length() != countrycodes[teststring.substr(0, 2)])
return false;
if (!all(teststring, is_alnum()))
return false;
to_upper(teststring);
std::rotate(teststring.begin(), teststring.begin() + 4, teststring.end());
std::string numberstring; // will contain the letter substitutions
for (const auto &c : teststring) {
if (std::isdigit(c))
numberstring += c;
if (std::isupper(c))
numberstring += std::to_string(static_cast<int>(c) - 55);
}
// implements a stepwise check for mod 97 in chunks of 9 at the first time,
// then in chunks of seven prepended by the last mod 97 operation converted
// to a string
int segstart = 0;
int step = 9;
std::string prepended;
long number = 0;
while (segstart < numberstring.length() - step) {
number = std::stol(prepended + numberstring.substr(segstart, step));
int remainder = number % 97;
prepended = std::to_string(remainder);
if (remainder < 10)
prepended = "0" + prepended;
segstart = segstart + step;
step = 7;
}
number = std::stol(prepended + numberstring.substr(segstart));
return (number % 97 == 1);
}
void SayValidity(const std::string &iban) {
std::cout << iban << (isValid(iban) ? " is " : " is not ") << "valid\n";
}
int main() {
SayValidity("GB82 WEST 1234 5698 7654 32");
SayValidity("GB82TEST12345698765432");
return 0;
}</syntaxhighlight>{{out}}
GB82 WEST 1234 5698 7654 32 is valid!
GB82TEST12345698765432 is not valid!
=={{header|Caché ObjectScript}}==
<
{
Line 943 ⟶ 1,121:
}
}</
<pre>USER>For { Read iban Quit:iban="" Write " => ", ##class(Utils.Validate).VerifyIBAN(iban), ! }
GB82 WEST 1234 5698 7654 32 => 1
Line 956 ⟶ 1,134:
=={{header|Clojure}}==
<
{"AL" 28 "AD" 24 "AT" 20 "AZ" 28 "BE" 16 "BH" 22 "BA" 20 "BR" 29
"BG" 22 "CR" 21 "HR" 21 "CY" 28 "CZ" 24 "DK" 18 "DO" 28 "EE" 20
Line 979 ⟶ 1,157:
(prn (valid-iban? "GB82 WEST 1234 5698 7654 32") ; true
(valid-iban? "GB82 TEST 1234 5698 7654 32")) ; false</
=={{header|COBOL}}==
{{works with|OpenCOBOL}}
<
PROGRAM-ID. iban-main.
Line 1,143 ⟶ 1,321:
END PROGRAM create-iban-number.
END PROGRAM validate-iban.</
<pre>
GB82 WEST 1234 5698 7654 32 is valid.
Line 1,150 ⟶ 1,328:
=={{header|Common Lisp}}==
<
;;
;; List of the IBAN code lengths per country.
Line 1,212 ⟶ 1,390:
(= 1 (mod (IBAN-to-integer X) 97)))))
(validp (remove #\Space iban))))
</syntaxhighlight>
'''Output:'''
<pre>
Line 1,225 ⟶ 1,403:
=={{header|D}}==
{{trans|Python}}
<
std.algorithm, std.ascii;
Line 1,261 ⟶ 1,439:
"GB82 TEST 1234 5698 7654 32"])
writefln("%s validation is: %s", account, account.validIBAN);
}</
<pre>GB82 WEST 1234 5698 7654 32 validation is: true
GB82 TEST 1234 5698 7654 32 validation is: false</pre>
=={{header|DBL}}==
<syntaxhighlight lang="dbl">;
; Validate IBAN for DBL version 4 by Dario B.
;
; Compile with "dbl -r" to reinitialize all data each time
; the module (main or subroutine) is entered.
;
RECORD
J, D5
ISVALID, D1
IBAN, A35,'GB82 WEST 1234 5698 7654 32'
, A35,'GB82 TEST 1234 5698 7654 32'
, A35,'GR16 0110 1250 0000 0001 2300 695'
, A35,'GB29 NWBK 6016 1331 9268 19'
, A35,'SA03 8000 0000 6080 1016 7519'
, A35,'CH93 0076 2011 6238 5295 7'
, A35,'IL62 0108 0000 0009 9999 999'
, A35,'US12 3456 7890 0987 6543 210'
, A35,'GR16 0110 1250 0000 0001 2300 695X'
PROC
;-------------------------------------------------------------------------------
XCALL FLAGS (0007000000,1) ;Suppress STOP message
OPEN (1,O,'TT:') ;Open video
FOR J=1 UNTIL 9
DO BEGIN
XCALL CHECK_IBAN (IBAN(J),ISVALID)
IF (ISVALID.EQ.1)
THEN DISPLAY (1,IBAN(J),' is valid',10)
ELSE DISPLAY (1,IBAN(J),' is not valid',10)
END
CLOSE 1
END
;===============================================================================
; CHECK AND VALIDATE IBAN SUBROUTINE
;===============================================================================
SUBROUTINE CHECK_IBAN
IBAN, A ;IBAN code
ISVALID, D ;0=is not valid 1=is valid
RECORD
K, D5
J, D5
D2, D2
D3, D3
D9, D9
A9, A9
IBANC, A35
IINT, A80
PARTS, D2
MOD, D5
.DEFINE NC,75 ;numbers of ISO / LEN in table
ISOCK, [NC]A4,'AD24','AE23','AL28','AT20','AZ28','BA20','BE16','BG22','BH22','BR29',
&'BY28','CH21','CR22','CY28','CZ24','DE22','DK18','DO28','EE20','ES24',
&'FI18','FO18','FR27','GB22','GE22','GI23','GL18','GR27','GT28','HR21',
&'HU28','IE22','IL23','IQ23','IS26','IT27','JO30','KW30','KZ20','LB28',
&'LC32','LI21','LT20','LU20','LV21','MC27','MD24','ME22','MK19','MR27',
&'MT31','MU30','NL18','NO15','PK24','PL28','PS29','PT25','QA29','RO24',
&'RS22','SA24','SC31','SE24','SI19','SK24','SM27','ST25','SV28','TL23',
&'TN24','TR26','UA29','VG24','XK20'
PROC
;-------------------------------------------------------------------------------
CLEAR IBANC,IINT ;Not required when compiled with dbl -r
;Remove blanks
K=
FOR J=1 UNTIL %TRIM(IBAN) DO IF (IBAN(J:1).NE.' ')
BEGIN
INCR K
IBANC(K:1)=IBAN(J:1)
END
;Check ISO code and len
UPCASE IBANC
FOR J=1 UNTIL NC DO IF (IBANC(1:2).EQ.ISOCK[J](1:2)) EXITLOOP
IF (J.GT.NC)
BEGIN
;ISO not found in table, returns ISVALID=0
ISVALID=
XRETURN
END
D2=ISOCK[J](3:2)
IF (%TRIM(IBANC).NE.D2)
BEGIN
;wrong len returns ISVALID=0
ISVALID=
XRETURN
END
; Now, do this
; IBAN: GB82WEST12345698765432
; Rearrange: WEST12345698765432GB82
; Convert to integer: 3214282912345698765432161182
; Compute remainder: 3214282912345698765432161182 mod 97 = 1
;Move the first four characters to the end
IBANC(%TRIM(IBANC)+1:4)=IBANC(1:4)
IBANC=IBANC(5,$LEN(IBANC))
;Convert A to 10, B to 11 ... Z
K=1
FOR J=1 UNTIL %TRIM(IBANC)
DO BEGIN
IF (IBANC(J:1).GE.'A'.AND.IBANC(J:1).LE.'Z')
THEN BEGIN
XCALL DECML (IBANC(J:1),D3)
IINT(K:2)=D3-55,'XX'
K=K+2
END
ELSE BEGIN
IINT(K:1)=IBANC(J:1)
INCR K
END
END
;Calculate the mod 97 of large number.
;Implements a stepwise check for mod 97 in chunks of 9 at the first time,
;then in chunks of seven prepended by the last mod 97 operation converted
;to a string
PARTS=1+(%TRIM(IINT)-9)/7
IF ((PARTS-1)*7.LT.%TRIM(IINT)-9) INCR PARTS
FOR J=1 UNTIL PARTS
DO BEGIN
IF (J.EQ.1) THEN A9=IINT(1:9)
ELSE BEGIN
A9(1:2)=MOD,'XX'
A9(3:7)=IINT(10+(J-2)*7:7)
END
D9=A9
MOD=D9-(D9/97)*97
END
IF (MOD.EQ.1) ISVALID=1
ELSE ISVALID=
XRETURN
END</syntaxhighlight>
{{out}}
<pre>GB82 WEST 1234 5698 7654 32 is valid
GB82 TEST 1234 5698 7654 32 is not valid
GR16 0110 1250 0000 0001 2300 695 is valid
GB29 NWBK 6016 1331 9268 19 is valid
SA03 8000 0000 6080 1016 7519 is valid
CH93 0076 2011 6238 5295 7 is valid
IL62 0108 0000 0009 9999 999 is valid
US12 3456 7890 0987 6543 210 is not valid
GR16 0110 1250 0000 0001 2300 695X is not valid</pre>
=={{header|Elixir}}==
{{trans|Ruby}}
<
@len %{ AL: 28, AD: 24, AT: 20, AZ: 28, BE: 16, BH: 22, BA: 20, BR: 29,
BG: 22, CR: 21, HR: 21, CY: 28, CZ: 24, DK: 18, DO: 28, EE: 20,
Line 1,302 ⟶ 1,648:
"GB82 TEST 1234 5698 7654 32",
"ZZ12 3456 7890 1234 5678 12" ]
|> Enum.each(fn iban -> IO.puts "#{IBAN.valid?(iban)}\t#{iban}" end)</
{{out}}
Line 1,315 ⟶ 1,661:
=={{header|F_Sharp|F#}}==
<
open System.Text.RegularExpressions
Line 1,366 ⟶ 1,712:
|> function | Some(_) -> "a valid IBAN" | None -> "an invalid IBAN"
|> printfn "%s is %s" iban
0</
>Rosetta.exe "GB82 WEST 1234 5698 7654 32"
GB82 WEST 1234 5698 7654 32 is a valid IBAN
Line 1,372 ⟶ 1,718:
>Rosetta.exe "GB82 TEST 1234 5698 7654 32"
GB82 TEST 1234 5698 7654 32 is an invalid IBAN
=={{header|Factor}}==
<syntaxhighlight lang="factor">USING: assocs combinators.short-circuit formatting kernel math
math.parser regexp sequences sets qw unicode ;
IN: rosetta-code.iban
<PRIVATE
CONSTANT: countries H{
{ 15 qw{ NO } }
{ 16 qw{ BE } }
{ 18 qw{ DK FO FI GL NL } }
{ 19 qw{ MK SI } }
{ 20 qw{ AT BA EE KZ LT LU } }
{ 21 qw{ CR HR LV LI CH } }
{ 22 qw{ BH BG GE DE IE ME RS GB } }
{ 23 qw{ GI IL AE } }
{ 24 qw{ AD CZ MD PK RO SA SK ES SE TN VG } }
{ 25 qw{ PT } }
{ 26 qw{ IS TR } }
{ 27 qw{ FR GR IT MR MC SM } }
{ 28 qw{ AL AZ CY DO GT HU LB PL } }
{ 29 qw{ BR PS } }
{ 30 qw{ KW MU } }
{ 31 qw{ MT } }
}
: valid-chars? ( str -- ? ) R/ [A-Z0-9]+/ matches? ;
: valid-length? ( str -- ? )
[ 2 head ] [ length ] bi countries at member? ;
: valid-checksum? ( str -- ? )
4 cut swap append [ digit> number>string ] { } map-as
concat string>number 97 mod 1 = ;
PRIVATE>
: valid-iban? ( str -- ? )
" " without {
[ valid-chars? ] [ valid-length? ] [ valid-checksum? ]
} 1&& ;
: iban-demo ( -- )
"GB82 WEST 1234 5698 7654 32"
"GB82 TEST 1234 5698 7654 32"
[
dup valid-iban? "may be a valid" "is an invalid" ?
"%s %s IBAN\n" printf
] bi@ ;
MAIN: iban-demo</syntaxhighlight>
{{out}}
<pre>
GB82 WEST 1234 5698 7654 32 may be a valid IBAN
GB82 TEST 1234 5698 7654 32 is an invalid IBAN
</pre>
=={{header|Forth}}==
{{Works with|4tH|3.62.3}}
<syntaxhighlight lang="text">include lib/ulcase.4th \ for S>UPPER
include lib/triple.4th \ for UT/MOD
include lib/cstring.4th \ for C/STRING
Line 1,412 ⟶ 1,815:
;
checkiban</
{{Out}}
<pre>linux:~> pp4th -x chkiban.4th
Line 1,422 ⟶ 1,825:
=={{header|Fortran}}==
<
program ibancheck
Line 1,547 ⟶ 1,950:
end program ibancheck
</syntaxhighlight>
{{out}}
Line 1,566 ⟶ 1,969:
=={{header|FreeBASIC}}==
<
' List updated to release 72, 25 November 2016, of IBAN Registry (75 countries)
Line 1,637 ⟶ 2,040:
Print
Print "Press any key to quit"
Sleep</
{{out}}
Line 1,644 ⟶ 2,047:
GB82 TEST 1234 5698 7654 32 : is not valid
</pre>
=={{header|Free Pascal}}==
{{libheader|GMP}}
<syntaxhighlight lang="delphi">{$mode objFPC}
uses
// for delSpace function
strUtils,
// GNU multiple-precision library
GMP;
var
/// this trie contains the correct IBAN length depending on the country code
IBANLengthInCountry: array['A'..'Z', 'A'..'Z'] of integer;
type
/// Human-readable representation of an IBAN without spaces
IBANRepresentation = string[34];
{
determines whether an IBANRepresentation is technically potentially correct
\param sample the human-readable IBAN representation
\return \param sample constains less than the most detectable errors
}
function isLegal(sample: IBANRepresentation): Boolean;
function checksumCorrect(sample: IBANRepresentation): Boolean;
type
digitSequence = string[68];
function convertLetters(const sequence: IBANRepresentation): digitSequence;
var
i: integer;
transliteration: string[2];
begin
// initialize length to zero
result := '';
for i := 1 to length(sequence) do
begin
// by default assume it is a Western-Arabic digit
transliteration := sequence[i];
// otherwise we’ll need to transliterate the Latin alphabet character
if not (sequence[i] in ['0'..'9']) then
begin
writeStr(transliteration, ord(sequence[i]) - ord('A') + 10);
end;
// appending transliteration to result also changes the length field
result := result + transliteration;
end;
end;
var
i, k: MPInteger;
begin
k := 97;
// put first four characters toward end
writeStr(sample, sample[5..length(sample)], sample[1..4]);
// interpret converted string as integer to base 10
Z_set_str(i, convertLetters(sample), 10);
i := Z_mod(i, k);
k := 1;
checksumCorrect := Z_cmp(i, k) = 0;
end;
begin
sample := upCase(sample);
{$push}
// just for emphasis: by default the FPC performs lazy evaluation
{$boolEval off}
isLegal := (length(sample) = IBANLengthInCountry[sample[1], sample[2]])
and checksumCorrect(sample);
{$pop}
end;
// === MAIN ==============================================================
begin
// for the sake of merely achieving the Rosetta Code Task
IBANLengthInCountry['G', 'B'] := 22;
// note, strings longer than 34 characters will be silently clipped
writeLn(isLegal(delSpace('GB82 WEST 1234 5698 7654 32')));
end.</syntaxhighlight>
{{out}}
<pre>TRUE</pre>
=={{header|Go}}==
<syntaxhighlight lang="go">package main
import (
"
"strings"
"
)
var
"AL": 28, "AD": 24, "AT": 20, "AZ": 28, "BE": 16, "BH": 22
"PL": 28, "PT": 25, "RO": 24, "SM": 27, "SA": 24, "RS": 22,
"SK": 24, "SI": 19, "ES": 24, "SE": 24, "CH": 21, "TN": 24,
"TR": 26, "AE": 23, "GB": 22, "VG": 24,
}
if len(iban) < 4 {
return false
}
iban = regexp.MustCompile(`\s+`).ReplaceAllString(iban, "")
iban = strings.ToUpper(iban)
if ibanLen := lengthByCountryCode[iban[0:2]]; ibanLen == len(iban) {
return false
}
iban = iban[4:] + iban[:4]
result := 0
for _, ch := range iban {
var n int
if '0' <= ch && ch <= '9' {
n = int(ch) - '0'
} else if 'A' <= ch && ch <= 'Z' {
n = 10 + int(ch) - 'A'
} else {
return false
}
if n < 10 {
result = (10*result + n) % 97
} else {
result = (100*result + n) % 97
}
}
return result == 1
}
func TestIsValidIBAN(t *testing.T) {
tests := []struct {
iban string
valid bool
}{
{"GB82 WEST 1234 5698 7654 32", true},
{"GB82 TEST 1234 5698 7654 32", false},
}
for _, test := range tests {
if isValidIBAN(test.iban) == test.valid {
return
}
t.Errorf("Expected %q to be %v", test.iban, test.valid)
}
}
</syntaxhighlight>
=={{header|Groovy}}==
<
def iso = [AL: 28, AD: 24, AT: 20, AZ: 28, BE: 16, BH: 22, BA: 20, BR: 29, BG: 22,
HR: 21, CY: 28, CZ: 24, DK: 18, DO: 28, EE: 20, FO: 18, FI: 18, FR: 27, GE: 22, DE: 22, GI: 23,
Line 1,733 ⟶ 2,223:
def number = iban.collect { Character.digit(it as char, 36) }.join('')
(number as BigInteger).mod(97) == 1
}</
Testing:
<
'GB82 TEST 1234 5698 7654 32',
'GB81 WEST 1234 5698 7654 32',
Line 1,742 ⟶ 2,232:
'CH93 0076 2011 6238 5295 7' ].each { iban ->
println "$iban is ${validateIBAN(iban) ? 'valid' : 'invalid'}"
}</
{{output}}
<pre>GB82 WEST 1234 5698 7654 32 is valid
Line 1,752 ⟶ 2,242:
=={{header|Haskell}}==
This program uses the Maybe and Either monads to handle failures. Values of type 'Maybe a' can contain 'Nothing' (no value) or 'Just a' (a value of type 'a'). Values of type 'Either a b' contain 'Left b' (usually indicating failure) or 'Right c' (usually indicating success).
<
import Data.Maybe (fromJust)
Line 1,804 ⟶ 2,294:
invalidBecause reason = Left $ "Invalid IBAN number " ++ xs ++
": " ++ reason</
<pre>
validateIBAN "GB82 WEST 1234 5698 7654 32"
Line 1,824 ⟶ 2,314:
Left "Invalid IBAN number GB82 _EST 1234 5698 7654 32: Number contains illegal digits."
</pre>
=={{header|IS-BASIC}}==
<syntaxhighlight lang="is-basic">100 PROGRAM "IBAN.bas"
110 STRING CO$(1 TO 93)*2
120 NUMERIC LG(1 TO 93)
130 FOR I=1 TO 93
140 READ CO$(I),LG(I)
150 NEXT
160 DO
170 PRINT :PRINT "IBAN code: ":INPUT PROMPT ">":IB$
180 IF IB$="" THEN EXIT DO
190 IF IBAN(IB$) THEN
200 PRINT "CRC ok."
210 ELSE
220 SET #102:INK 3:PRINT "CRC error.":SET #102:INK 1
230 END IF
240 LOOP
250 DEF TRIM$(S$)
260 LET T$=""
270 FOR I=1 TO LEN(S$)
280 IF S$(I)>CHR$(47) AND S$(I)<CHR$(91) THEN LET T$=T$&S$(I)
290 NEXT
300 LET TRIM$=T$
310 END DEF
320 DEF IBAN(IB$)
330 LET IB$=TRIM$(UCASE$(IB$)):LET T$="":LET M,IBAN=0:LET N=FIND(IB$(1:2))
340 IF N=0 THEN PRINT "Invalid country code.":EXIT DEF
350 IF LEN(IB$)<>LG(N) THEN PRINT "Number length does not match.":EXIT DEF
360 LET IB$=IB$&IB$(1:4):LET IB$=IB$(5:)
370 CALL CONVERT(IB$)
380 FOR I=1 TO LEN(IB$)
390 LET T$=STR$(M)&IB$(I)
400 LET M=MOD(VAL(T$),97)
410 NEXT
420 IF M=1 THEN LET IBAN=-1
430 END DEF
440 DEF FIND(S$)
450 LET FIND=0
460 LET BO=LBOUND(CO$):LET UP=UBOUND(CO$)
470 DO
480 LET K=INT((BO+UP)/2)
490 IF CO$(K)<S$ THEN LET BO=K+1
500 IF CO$(K)>S$ THEN LET UP=K-1
510 LOOP WHILE BO<=UP AND CO$(K)<>S$
520 IF BO<=UP THEN LET FIND=K
530 END DEF
540 DEF CONVERT(REF S$)
550 LET T$=""
560 FOR I=1 TO LEN(S$)
570 IF S$(I)>CHR$(64) AND S$(I)<CHR$(91) THEN
580 LET T$=T$&STR$(ORD(S$(I))-55)
590 ELSE
600 LET T$=T$&S$(I)
610 END IF
620 NEXT
630 LET S$=T$
640 END DEF
650 DATA AD,24,AE,23,AL,28,AO,25,AT,20,AZ,28,BA,20,BE,16,BF,28,BG,22,BH,22,BI,16,BJ,28,BR,29,BY,28,CG,27,CH,21,CI,28,CM,27,CR,22,CV,25,CY,28,CZ,24,DE,22,DK,18,DO,28,DZ,24,EE,20,EG,27,ES,24,FI,18,FO,18,FR,27,GA,27,GB,22,GE,22,GI,23,GL,18
660 DATA GR,27,GT,28,HN,28,HR,21,HU,28,IE,22,IL,23,IR,26,IS,26,IT,27,JO,30,KM,27,KW,30,KZ,20,LB,28,LI,21,LT,20,LU,20,LV,21,MA,28,MC,27,MD,24,ME,22,MG,27,MK,19,ML,28,MR,27,MT,31,MU,30,MZ,25,NE,28,NI,32,NL,18,NO,15,PK,24,PL,28,PS,29,PT,25
670 DATA QA,29,RO,24,RS,22,SA,24,SE,24,SI,19,SK,24,SM,27,SN,28,TD,27,TG,28,TL,23,TN,24,TR,26,UA,29,VG,24,XK,20</syntaxhighlight>
=={{header|J}}==
<
delblk =. #~ ' '&~:
NB. rearrange
Line 1,860 ⟶ 2,410:
NB. IBAN
iban =: quads @: ibancheck
</
<pre>
iban 'GB82 WEST 1234 5698 7654 32'
Line 1,875 ⟶ 2,425:
=={{header|Java}}==
{{works with|Java|8+}}
<
import java.util.*;
Line 1,925 ⟶ 2,475:
return bigInt.mod(BigInteger.valueOf(97)).intValue() == 1;
}
}</
<pre>GB82 WEST 1234 5698 7654 32 is valid.
GB82 TEST 1234 5698 7654 32 is not valid.
Line 1,937 ⟶ 2,487:
=={{header|JavaScript}}==
<
NO:15, BE:16, DK:18, FI:18, FO:18, GL:18, NL:18, MK:19,
SI:19, AT:20, BA:20, EE:20, KZ:20, LT:20, LU:20, CR:21,
Line 1,964 ⟶ 2,514:
document.write(isValid('GB82 TEST 1234 5698 7654 32'), '<br>') // false
document.write(isValid('SA03 8000 0000 6080 1016 7519'), '<br>') // true
</syntaxhighlight>
{{out}}
true
Line 1,975 ⟶ 2,525:
This implementation requires a version of jq with <tt>gsub</tt>.
The heart of the matter consists of just four lines of straightforward jq code:<
# strip the input string of spaces and tabs:
gsub("[ \t]";"")
Line 1,984 ⟶ 2,534:
# check the mod 97 criterion:
and ( (.[4:] + .[0:4]) | letters2digits | remainder) == 1
</syntaxhighlight>
This conciseness is achieved courtesy of the helper functions: <tt>letters2digits</tt> and <tt>remainder</tt>. These could be implemented as inner functions of the main function,
but for clarity they are shown as top-level functions here.
<
65 as $A | 90 as $Z
| ($A - 10) as $ten
Line 2,002 ⟶ 2,552:
# output: its remainder modulo 97 as a number
def remainder:
if length < 15 then
else (.[0:14] | remainder | tostring) as $r1
| ($r1 + .[14:]) | remainder
Line 2,024 ⟶ 2,574:
and length == $lengths[.[0:2]]
# Shift and convert:
and ( (.[4:] + .[0:4]) | letters2digits | remainder) == 1 ;</
"GB82 WEST 1234 5698 7654 32" | is_valid_iban #=> true
"GB82 TEST 1234 5698 7654 32" | is_valid_iban #=> false</
=={{header|Jsish}}==
From the Javascript entry.
<syntaxhighlight lang="javascript">var ibanLen = {
NO:15, BE:16, DK:18, FI:18, FO:18, GL:18, NL:18, MK:19,
SI:19, AT:20, BA:20, EE:20, KZ:20, LT:20, LU:20, CR:21,
CH:21, HR:21, LI:21, LV:21, BG:22, BH:22, DE:22, GB:22,
GE:22, IE:22, ME:22, RS:22, AE:23, GI:23, IL:23, AD:24,
CZ:24, ES:24, MD:24, PK:24, RO:24, SA:24, SE:24, SK:24,
VG:24, TN:24, PT:25, IS:26, TR:26, FR:27, GR:27, IT:27,
MC:27, MR:27, SM:27, AL:28, AZ:28, CY:28, DO:28, GT:28,
HU:28, LB:28, PL:28, BR:29, PS:29, KW:30, MU:30, MT:31
};
function isIBAN(iban) {
var i,m,s;
iban = iban.replace(/\s/g, '');
if (!iban.match(/^[0-9A-Z]+$/)) return false;
var len = iban.length;
if (len != ibanLen[iban.substr(0,2)]) return false;
iban = iban.substr(4) + iban.substr(0,4);
for (s = '', i = 0; i < len; i += 1) s += parseInt(iban.charAt(i),36);
for (m = s.substr(0, 15) % 97, s = s.substr(15); s; s = s.substr(13)) m = (m + s.substr(0,13)) % 97;
return m == 1;
}
;isIBAN('GB82 WEST 1234 5698 7654 32');
;isIBAN('GB82 WEST 1.34 5698 7654 32');
;isIBAN('GB82 WEST 1234 5698 7654 325');
;isIBAN('GB82 TEST 1234 5698 7654 32');
;isIBAN('SA03 8000 0000 6080 1016 7519');
/*
=!EXPECTSTART!=
isIBAN('GB82 WEST 1234 5698 7654 32') ==> true
isIBAN('GB82 WEST 1.34 5698 7654 32') ==> false
isIBAN('GB82 WEST 1234 5698 7654 325') ==> false
isIBAN('GB82 TEST 1234 5698 7654 32') ==> false
isIBAN('SA03 8000 0000 6080 1016 7519') ==> true
=!EXPECTEND!=
*/</syntaxhighlight>
{{out}}
<pre>prompt$ jsish -u IBAN.jsi
[PASS] IBAN.jsi</pre>
=={{header|Julia}}==
{{works with|Julia|0.6}}
<
country2length = Dict(
"AL" => 28, "AD" => 24, "AT" => 20, "AZ" => 28, "BE" => 16, "BH" => 22, "BA" => 20, "BR" => 29,
Line 2,052 ⟶ 2,647:
digs = parse(BigInt, join(parse(Int, ch, 36) for ch in iban))
return rst && digs % 97 == 1
end</
{{out}}
Line 2,059 ⟶ 2,654:
=={{header|Kotlin}}==
<
import java.math.BigInteger
object
/* List updated to release 73, January 2017, of IBAN Registry (75 countries) */
private const val countryCodes = "" +
"AD24 AE23 AL28 AT20 AZ28 BA20 BE16 BG22 BH22 BR29
"
"MT31 MU30 NL18 NO15 PK24 PL28 PS29 PT25 QA29 RO24 " +
"RS22 SA24 SC31 SE24 SI19 SK24 SM27 ST25 SV28 TL23 " +
"TN24 TR26 UA29 VG24 XK20"
// remove spaces from IBAN
var s = iban.replace(" ", "")
// check country code and length
// move first 4 characters to
s = s.substring(4) + s.substring(0, 4)
// replace A to Z with numbers 10 To 35
// check whether mod 97 calculation gives a remainder of 1
return BigInteger(s) % BigInteger.valueOf(97L) == BigInteger.ONE
}
}
fun main(
val ibans = arrayOf(
"GB82 WEST 1234 5698 7654 32",
"GB82 TEST 1234 5698 7654 32"
)
for (iban in ibans) {
}
}</
{{out}}
Line 2,105 ⟶ 2,704:
GB82 TEST 1234 5698 7654 32 is not valid
</pre>
=== With IBAN REGISTRY parser ===
<syntaxhighlight lang="kotlin">
package rosettacode
import rosettacode.Outcome.Fail
import rosettacode.Outcome.Ok
import java.lang.IllegalArgumentException
import java.math.BigInteger
sealed class Outcome {
object Ok : Outcome() {
override fun toString(): String = "Ok"
}
data class Fail(val error: String) : Outcome()
}
fun Boolean.asOutcome(error: String = "") = when (this) {
true -> Ok
false -> Fail(error)
}
/**
* A validator based on a pattern (e.g GB2!n4!a6!n8!n) as defined in the IBAN REGISTRY
* Doesn't handle variable length IBANs - sufficient for Rosetta
*
* IBAN REGISTRY: https://www.swift.com/swift_resource/9606
*/
class Validator(private val pattern: String) {
private val triplet: Regex = """(\d+)!([n,a,c])""".toRegex() // e.g: 10!n
private val rules: List<Regex>
init {
val countTypePairs = triplet
.findAll(pattern)
.map { Pair(it.groups[1]!!.value.toInt(), it.groups[2]!!.value) }
rules = countTypePairs.fold(emptyList()) { acc, p ->
acc + when (p.second) {
"n" -> """[0-9]{${p.first}}""".toRegex()
"a" -> """[A-Z]{${p.first}}""".toRegex()
"c" -> """[A-Z,0-9]{${p.first}}""".toRegex()
else -> throw IllegalArgumentException("Unexpected pattern: $pattern")
}
}
}
fun isValid(ccbban: String): Outcome {
var k = 0
for (rule in rules) {
when (val match = rule.matchAt(ccbban, k)) {
null -> return Fail("failed rule: $rule")
else -> {
k = match.range.last + 1
}
}
}
return (k == ccbban.length).asOutcome("IBAN is too long")
}
}
object Registry : MutableMap<String, Validator> by mutableMapOf()
fun String.split(i: Int) = Pair(this.substring(0, i), this.substring(i))
fun String.toDigits() = this.fold("") { acc, ch -> acc + Character.getNumericValue(ch) }
fun Pair<String, String>.swap() = this.second + this.first
object CDCheck {
private val _97 = BigInteger.valueOf(97)
private val _1 = BigInteger.ONE
operator fun invoke(digits: String) = (BigInteger(digits).mod(_97) == _1).asOutcome("Invalid Check Digits")
}
fun validate(iban: String): Outcome {
val normalized = iban.replace(" ", "").uppercase().also {
if (it.length < 5) {
return Fail("IBAN is too short")
}
}
val (cc, tail) = normalized.split(2)
val validator = Registry[cc] ?: return Fail("Unknown Country Code: $cc")
return when (val outcome = validator.isValid(tail)) {
is Fail -> outcome
is Ok -> {
val allDigits = normalized
.split(4).swap() // (CC + CD) -> end
.toDigits() // letters -> digits
CDCheck(allDigits)
}
}
}
fun main() {
Registry["GB"] = Validator("GB2!n4!a6!n8!n")
Registry["DE"] = Validator("DE2!n8!n10!n")
validate("GB82WEST12345698765432").also { println(it) } // Ok
validate("GB82 WEST 1234 5698 7654 32").also { println(it) } // Ok
validate("DE89370400440532013000").also { println(it) } // Ok
validate("XX82WEST1234569A765432").also { println(it) } // Unknown Country Code: XX
validate("GB82").also { println(it) } // IBAN is too short
validate("GB82WEST1234569A765432").also { println(it) } // failed rule: [0-9]{8}
validate("GB82WE5T1234569A765432").also { println(it) } // failed rule: [A-Z]{4}
validate("GB82WEST123456987654329").also { println(it) } // IBAN is too long
validate("GB80WEST12345698765432").also { println(it) } // Invalid Check Digits
}
</syntaxhighlight>
{{out}}
<pre>
Ok
Ok
Ok
Fail(error=Unknown Country Code: XX)
Fail(error=IBAN is too short)
Fail(error=failed rule: [0-9]{8})
Fail(error=failed rule: [A-Z]{4})
Fail(error=IBAN is too long)
Fail(error=Invalid Check Digits)
</pre>
=={{header|Lobster}}==
<syntaxhighlight lang="lobster">
let cc = ["AD","AE","AL","AO","AT","AZ","BA","BE","BF","BG","BH","BI","BJ","BR","CG","CH","CI","CM","CR","CV","CY",
"CZ","DE","DK","DO","DZ","EE","EG","ES","FI","FO","FR","GA","GB","GE","GI","GL","GR","GT","HR","HU","IE",
"IL","IR","IS","IT","JO","KW","KZ","LB","LI","LT","LU","LV","MC","MD","ME","MG","MK","ML","MR","MT","MU",
"MZ","NL","NO","PK","PL","PS","PT","QA","RO","RS","SA","SE","SI","SK","SM","SN","TN","TR","UA","VG"]
let ln = [ 24, 23, 28, 25, 20, 28, 20, 16, 27, 22, 22, 16, 28, 29, 27, 21, 28, 27, 21, 25, 28,
24, 22, 18, 28, 24, 20, 27, 24, 18, 18, 27, 27, 22, 22, 23, 18, 27, 28, 21, 28, 22,
23, 26, 26, 27, 30, 30, 20, 28, 21, 20, 20, 21, 27, 24, 22, 27, 19, 28, 27, 31, 30,
25, 18, 15, 24, 28, 29, 25, 29, 24, 22, 24, 24, 19, 24, 27, 28, 24, 26, 29, 24 ]
def ccToLen(s: string) -> int:
let cnt, idx = binary_search(cc, s.substring(0, 2))
if cnt == 1:
return ln[idx]
else:
return -1
def strip(s: string) -> string:
let t = s.tokenize(" ", " ") // splits on spaces and trims spaces from segments
return t.concat_string("") // joins it back together
def rotmod97step(accumu: int, c: int) -> int:
accumu *= 10
if '0' <= c and c <= '9':
accumu += c - '0'
else: if 'A' <= c and c <= 'Z':
accumu *= 10
accumu += c + 10 - 'A'
else:
return -1
while accumu >= 97:
accumu -= 97
return accumu
def rotmod97(s: string) -> int:
// the first four chars come last
// all chars from 'A' to 'Z' => "10" to "35"
let len = s.length
var accumu = 0 // result; negative indicates error
var i = 4 // starting index
while i < len and accumu >= 0:
accumu = rotmod97step(accumu, s[i])
i += 1
i = 0
while i < 4 and accumu >= 0:
accumu = rotmod97step(accumu, s[i])
i += 1
return accumu
def isValidIBAN(iban: string) -> bool:
let s = strip(uppercase(iban))
if s.length == ccToLen(s):
return rotmod97(s) == 1
else:
return false
assert isValidIBAN("GB82 WEST 1234 5698 7654 32")
assert isValidIBAN("GB82 West 1234 5698 7654 32")
assert not isValidIBAN("GB82 TEST 1234 5698 7654 32")
assert not isValidIBAN("GB82 WEST 1243 5698 7654 32")
</syntaxhighlight>
=={{header|Logtalk}}==
<
:- object(iban).
Line 2,237 ⟶ 3,028:
:- end_object.
</syntaxhighlight>
Testing:
<
| ?- iban::valid("GB82 WEST 1234 5698 7654 32").
yes
</syntaxhighlight>
=={{header|Lua}}==
<
{
AL=28, AD=24, AT=20, AZ=28, BH=22, BE=16, BA=20, BR=29, BG=22, CR=21,
Line 2,268 ⟶ 3,059:
end
return mod==1
end</
=={{header|M2000 Interpreter}}==
We make a lambda function which return a string, with the input IBAN plus (Valid) or (Invalid) at the end.
<syntaxhighlight lang="m2000 interpreter">
\\ IBAN checker
Function MakeIBANfun$ {
Inventory countrylength = "AL" := 28, "AD" := 24, "AT" := 20, "AZ" := 28, "BE" := 16, "BH" := 22, "BA" := 20, "BR" := 29
Append countrylength, "BG" := 22, "CR" := 21, "HR" := 21, "CY" := 28, "CZ" := 24, "DK" := 18, "DO" := 28, "EE" := 20
Append countrylength, "FO" := 18, "FI" := 18, "FR" := 27, "GE" := 22, "DE" := 22, "GI" := 23, "GR" := 27, "GL" := 18
Append countrylength, "GT" := 28, "HU" := 28, "IS" := 26, "IE" := 22, "IL" := 23, "IT" := 27, "KZ" := 20, "KW" := 30
Append countrylength, "LV" := 21, "LB" := 28, "LI" := 21, "LT" := 20, "LU" := 20, "MK" := 19, "MT" := 31, "MR" := 27
Append countrylength, "MU" := 30, "MC" := 27, "MD" := 24, "ME" := 22, "NL" := 18, "NO" := 15, "PK" := 24, "PS" := 29
Append countrylength, "PL" := 28, "PT" := 25, "RO" := 24, "SM" := 27, "SA" := 24, "RS" := 22, "SK" := 24, "SI" := 19
Append countrylength, "ES" := 24, "SE" := 24, "CH" := 21, "TN" := 24, "TR" := 26, "AE" := 23, "GB" := 22, "VG" := 24
=Lambda$ countrylength (Iban0$)->{
Iban$=Filter$(Ucase$(Iban0$), " ")
Iban$=Filter$(Iban$, Filter$(Iban$,"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"))
Def Decimal ch, c
{
If Not Exist(countrylength, Left$(Iban$,2)) Then Exit
length=Eval(countrylength)
If Not Len(Iban$)=length Then exit
Buffer ScanChar as Integer*length
Return ScanChar, 0:=Mid$(Iban$,5), length-4:=Mid$(Iban$,1,4)
For i=0 to length-1 {
ch=Eval(ScanChar, i)
if ch>=48 and ch<=57 then {
c = c*10+ch-48
} else.if ch>=65 and ch<=90 then {
c = c*100+ch-55
} else c=-1: exit
}
c = c mod 97
}
=Iban0$ + If$(c=1 ->" (Valid)", " (Invalid)")
}
}
IbanCheck$=MakeIBANfun$()
Print IbanCheck$("GB82 WEST 1234 5698 7654 32") ' valid
Print IbanCheck$("GB82 TEST 1234 5698 7654 32")
Print IbanCheck$("SA03 8000 0000 6080 1016 7519") ' valid
Print IbanCheck$("GR16 0110 1250 0000 0001 2300 695X")
Print IbanCheck$("MK11 2222 3333 4444 555")
</syntaxhighlight>
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<
ClearAll[IBANVerify]
IBANVerify[input_String]:=Module[{i,cc,rules},
Line 2,299 ⟶ 3,137:
False
]
]</
Trying out the function:
<
IBANVerify["GB82 WEST 1234 5698 7654 323"]
IBANVerify["GB82 WEST 1234 5698 7654 31"]
IBANVerify["XX82 WEST 1234 5698 7654 323"]</
True
False
Line 2,312 ⟶ 3,150:
=={{header|MATLAB}}==
Didn't check country codes, lengths, or consistent checksums to keep this applicable to any new countries.
<
% Determine if International Bank Account Number is valid IAW ISO 13616
% iban - string containing account number
Line 2,341 ⟶ 3,179:
i2 = min(i1+8, nDig);
end
end</
<
'GB82 TEST 1234 5698 7654 32' ;
'CH93 0076 2011 6238 5295 7' ;
Line 2,352 ⟶ 3,190:
for k = 1:length(tests)
fprintf('%s -> %svalid\n', tests{k}, char(~validateIBAN(tests{k}).*'in'))
end</
<pre>GB82 WEST 1234 5698 7654 32 -> valid
GB82 TEST 1234 5698 7654 32 -> invalid
Line 2,363 ⟶ 3,201:
=={{header|NewLISP}}==
<syntaxhighlight lang="newlisp">
(setq *iban-code-length* '((15 ("NO"))
(16 ("BE"))
Line 2,431 ⟶ 3,269:
)
)
</syntaxhighlight>
Output:
<pre>
Line 2,443 ⟶ 3,281:
=={{header|Nim}}==
{{libheader|bigints}}
<
let countryLen = toTable({
Line 2,476 ⟶ 3,314:
for account in ["GB82 WEST 1234 5698 7654 32", "GB82 TEST 1234 5698 7654 32"]:
echo account, " validation is: ", validIban account</
GB82 WEST 1234 5698 7654 32 validation is: true
GB82 TEST 1234 5698 7654 32 validation is: false
Line 2,482 ⟶ 3,320:
=={{header|Oberon-2}}==
Works with oo2c Version 2
<
MODULE IBAN;
IMPORT
Line 2,644 ⟶ 3,482:
Test;
END IBAN.
</syntaxhighlight>
Output:
<pre>
Line 2,652 ⟶ 3,490:
IBAN[XX0380000000608010167519]=Country XX not has IBAN codes. FALSE
</pre>
=={{header|OCaml}}==
{{works with|OCaml|4.03+}}
<
#load "str.cma"
#load "nums.cma" (* for module Big_int *)
Line 2,763 ⟶ 3,600:
in
List.iter (fun pair -> testit pair) ibans
</syntaxhighlight>
{{out}}
GB82 WEST 1234 5698 7654 32 is valid. Expected true [PASS]
Line 2,772 ⟶ 3,609:
CH93 0076 2011 6238 5295 7 is valid. Expected true [PASS]
"Completely incorrect iban" is not valid. Expected false [PASS]
=={{header|Perl}}==
<
use strict ;
use warnings ;
Line 2,822 ⟶ 3,658:
if ( validate_iban( "GB82TEST12345698765432" ) ) {
print "GB82TEST12345698765432 is valid!\n" ;
}</
GB82 WEST 1234 5698 7654 32 is a valid IBAN number!
GB82TEST12345698765432 is invalid!
=={{header|Phix}}==
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #000080;font-style:italic;">--
-- demo\rosetta\IBAN.exw
-- =====================
--</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">nations</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{{</span><span style="color: #008000;">"AD"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">24</span><span style="color: #0000FF;">},</span> <span style="color: #000080;font-style:italic;">-- Andorra
<span style="color: #0000FF;">{</span><span style="color: #008000;">"CH"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">21</span><span style="color: #0000FF;">},</span> <span style="color: #000080;font-style:italic;">-- Switzerland</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"GB"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">22</span><span style="color: #0000FF;">},</span> <span style="color: #000080;font-style:italic;">-- United Kingdom</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"SA"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">24</span><span style="color: #0000FF;">},</span> <span style="color: #000080;font-style:italic;">-- Saudi Arabia </span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"XK"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">20</span><span style="color: #0000FF;">}}</span> <span style="color: #000080;font-style:italic;">-- Kosovo</span>
<span style="color: #008080;">constant</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">countries</span><span style="color: #0000FF;">,</span><span style="color: #000000;">lengths</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">columnize</span><span style="color: #0000FF;">(</span><span style="color: #000000;">nations</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">iban</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">code</span><span style="color: #0000FF;">)</span>
<span style="color: #000080;font-style:italic;">-- This routine does and should reject codes containing spaces etc.
-- Use iban_s() below for otherwise.</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">country</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #000000;">code</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..</span><span style="color: #000000;">2</span><span style="color: #0000FF;">],</span><span style="color: #000000;">countries</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">country</span><span style="color: #0000FF;">!=</span><span style="color: #000000;">0</span>
<span style="color: #008080;">and</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">code</span><span style="color: #0000FF;">)=</span><span style="color: #000000;">lengths</span><span style="color: #0000FF;">[</span><span style="color: #000000;">country</span><span style="color: #0000FF;">]</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">code</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">code</span><span style="color: #0000FF;">[</span><span style="color: #000000;">5</span><span style="color: #0000FF;">..$]&</span><span style="color: #000000;">code</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..</span><span style="color: #000000;">4</span><span style="color: #0000FF;">]</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">c</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">code</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">ch</span><span style="color: #0000FF;">=</span><span style="color: #000000;">code</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">ch</span><span style="color: #0000FF;">>=</span><span style="color: #008000;">'0'</span> <span style="color: #008080;">and</span> <span style="color: #000000;">ch</span><span style="color: #0000FF;"><=</span><span style="color: #008000;">'9'</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">c</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">c</span><span style="color: #0000FF;">*</span><span style="color: #000000;">10</span><span style="color: #0000FF;">+</span><span style="color: #000000;">ch</span><span style="color: #0000FF;">-</span><span style="color: #008000;">'0'</span>
<span style="color: #008080;">elsif</span> <span style="color: #000000;">ch</span><span style="color: #0000FF;">>=</span><span style="color: #008000;">'A'</span> <span style="color: #008080;">and</span> <span style="color: #000000;">ch</span><span style="color: #0000FF;"><=</span><span style="color: #008000;">'Z'</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">c</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">c</span><span style="color: #0000FF;">*</span><span style="color: #000000;">100</span><span style="color: #0000FF;">+</span><span style="color: #000000;">ch</span><span style="color: #0000FF;">-</span><span style="color: #000000;">55</span>
<span style="color:
<span style="color: #008080;">return</span> <span style="color: #004600;">false</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #000000;">c</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mod</span><span style="color: #0000FF;">(</span><span style="color: #000000;">c</span><span style="color: #0000FF;">,</span><span style="color: #000000;">97</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">c</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">return</span> <span style="color: #004600;">false</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">iban_s</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">code</span><span style="color: #0000FF;">)</span>
<span style="color: #000080;font-style:italic;">-- strips any embedded spaces and hyphens before validating.</span>
<span style="color: #000000;">code</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">substitute_all</span><span style="color: #0000FF;">(</span><span style="color: #000000;">code</span><span style="color: #0000FF;">,{</span><span style="color: #008000;">" "</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"-"</span><span style="color: #0000FF;">},{</span><span style="color: #008000;">""</span><span style="color: #0000FF;">,</span><span style="color: #008000;">""</span><span style="color: #0000FF;">})</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">iban</span><span style="color: #0000FF;">(</span><span style="color: #000000;">code</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">procedure</span> <span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">code</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">bool</span> <span style="color: #000000;">expected</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">bool</span> <span style="color: #000000;">valid</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">iban_s</span><span style="color: #0000FF;">(</span><span style="color: #000000;">code</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">state</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">valid</span><span style="color: #0000FF;">=</span><span style="color: #000000;">expected</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">state</span> <span style="color: #0000FF;">=</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">valid</span><span style="color: #0000FF;">?</span><span style="color: #008000;">"ok"</span><span style="color: #0000FF;">:</span><span style="color: #008000;">"invalid (as expected)"</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">else</span>
<span style="color: #000000;">state</span> <span style="color: #0000FF;">=</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">valid</span><span style="color: #0000FF;">?</span><span style="color: #008000;">"OK!!"</span><span style="color: #0000FF;">:</span><span style="color: #008000;">"INVALID!!"</span><span style="color: #0000FF;">)&</span><span style="color: #008000;">" (NOT AS EXPECTED)"</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%-34s :%s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">code</span><span style="color: #0000FF;">,</span><span style="color: #000000;">state</span><span style="color: #0000FF;">})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"GB82 WEST 1234 5698 7654 32"</span><span style="color: #0000FF;">,</span><span style="color: #004600;">true</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"GB82 TEST 1234 5698 7654 32"</span><span style="color: #0000FF;">,</span><span style="color: #004600;">false</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"GB81 WEST 1234 5698 7654 32"</span><span style="color: #0000FF;">,</span><span style="color: #004600;">false</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"SA03 8000 0000 6080 1016 7519"</span><span style="color: #0000FF;">,</span><span style="color: #004600;">true</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"CH93 0076 2011 6238 5295 7"</span><span style="color: #0000FF;">,</span><span style="color: #004600;">true</span><span style="color: #0000FF;">)</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
Line 2,920 ⟶ 3,734:
=={{header|PHP}}==
<
<?php
Line 3,000 ⟶ 3,814:
echo "IBAN NOT VALID";
}
</syntaxhighlight>
=={{header|Picat}}==
<syntaxhighlight lang="picat">go =>
IBAN = "GB82 WEST 1234 5698 7654 32",
println(IBAN=iban(IBAN)),
nl.
iban(IBAN) = Ret =>
nations(Nations),
Ret = [],
IBAN2 = IBAN.delete_all(' ').to_uppercase(),
Len = IBAN2.length,
Len > 2, % sanity check
if Len != Nations.get(slice(IBAN2,1,2),0).to_integer() then
Ret := ["wrong country length"]
end,
IBAN3 = slice(IBAN2,5,Len) ++ slice(IBAN2,1,4),
if [convert(C) : C in IBAN3].join('').to_integer() mod 97 != 1 then
Ret := Ret ++ ["bad mod"]
end,
if Ret == [] then Ret := "ok" end.
% convert char: 1=1,...9=9, A=10, B=11,Z=35
convert(C) = cond(ord(C)-65 >= 0,ord(C)-55,ord(C)-48).to_string().
% Lengths for different nations.
nations(Nations) =>
Nations = new_map(
["AL"=28, "AD"=24, "AT"=20, "AZ"=28, "BH"=22, "BE"=16, "BA"=20, "BR"=29, "BG"=22, "CR"=21,
"HR"=21, "CY"=28, "CZ"=24, "DK"=18, "DO"=28, "EE"=20, "FO"=18, "FI"=18, "FR"=27, "GE"=22,
"DE"=22, "GI"=23, "GR"=27, "GL"=18, "GT"=28, "HU"=28, "IS"=26, "IE"=22, "IL"=23, "IT"=27,
"JO"=30, "KZ"=20, "KW"=30, "LV"=21, "LB"=28, "LI"=21, "LT"=20, "LU"=20, "MK"=19, "MT"=31,
"MR"=27, "MU"=30, "MC"=27, "MD"=24, "ME"=22, "NL"=18, "NO"=15, "PK"=24, "PS"=29, "PL"=28,
"PT"=25, "QA"=29, "RO"=24, "SM"=27, "SA"=24, "RS"=22, "SK"=24, "SI"=19, "ES"=24, "SE"=24,
"CH"=21, "TN"=24, "TR"=26, "AE"=23, "GB"=22, "VG"=24]).</syntaxhighlight>
{{out}}
<pre>GB82 WEST 1234 5698 7654 32 = ok</pre>
===Some more (and interesting) tests===
<syntaxhighlight lang="picat">go2 =>
Ibans = ["GB82 WEST 1234 5698 7654 32",
"gb82 WEST 1234 5698 7654 32",
"GB82WEST12345698765432",
"GB82 WEST 234 5698 7654 32",
"GB82 WEST 1234 5698 7654 33",
"AE82 WEST 1234 5698 7654 32",
"GB82 TEST 1234 5698 7654 32",
"GR16 0110 1250 0000 0001 2300 695",
"GB29 NWBK 6016 1331 9268 19",
"SA03 8000 0000 6080 1016 7519",
"CH93 0076 2011 6238 5295 7",
"IL62 0108 0000 0009 9999 999"
],
foreach(IBAN in Ibans)
println(IBAN=iban(IBAN))
end,
nl.</syntaxhighlight>
{{out}}
<pre>GB82 WEST 1234 5698 7654 32 = ok
gb82 WEST 1234 5698 7654 32 = ok
GB82WEST12345698765432 = ok
GB82 WEST 234 5698 7654 32 = [wrong country length,bad mod]
GB82 WEST 1234 5698 7654 33 = [bad mod]
AE82 WEST 1234 5698 7654 32 = [wrong country length,bad mod]
GB82 TEST 1234 5698 7654 32 = [bad mod]
GR16 0110 1250 0000 0001 2300 695 = ok
GB29 NWBK 6016 1331 9268 19 = ok
SA03 8000 0000 6080 1016 7519 = ok
CH93 0076 2011 6238 5295 7 = ok
IL62 0108 0000 0009 9999 999 = ok</pre>
=={{header|PicoLisp}}==
<
(de iban (Str)
Line 3,032 ⟶ 3,920:
(println 'Invalid) ) )
(bye)</
=={{header|PowerShell}}==
<syntaxhighlight lang="powershell">
function verifIBAN ([string]$ibanS)
{
if ($ibanS.Length -ne 27) {return $false} else
{
$ibanI="$($ibanS.Substring(4,23))$($ibanS.Substring(0,4))".ToUpper()
[int]$comptIBAN=0
$NumIBAN=""
while ($comptIBAN -lt 27)
{
if ([byte]$ibanI[$comptIBAN] -ge 65 -and [byte]$ibanI[$comptIBAN] -le 90)
{
$NumIban+=([byte]$ibanI[$comptIBAN]-55)
} #pour transformer les lettres en chiffres (A=10, B=11...)
else
{
$NumIban+=$ibanI[$comptIBAN]
}
$comptIBAN++
}
#cela fait un nombre de 30 chiffres : trop pour powershell, je découpe donc les 9 premiers caractères :
if ("$($NumIBAN.Substring(0,9)%97)$($NumIBAN.Substring(9,$NumIBAN.Length-9))"%97 -eq 1)
{return $true}
else
{return $false}
}
} #fin fonction vérification IBAN / Stéphane RABANY 2018
</syntaxhighlight>
Ancien texte qui ne sert à rien selon moi :
I have heard that Regex should not be used with IBAN codes. Regex does seem to work, however.
<syntaxhighlight lang="powershell">
@'
"Country","Length","Example"
Line 3,097 ⟶ 4,015:
$ibans
</syntaxhighlight>
{{Out}}
<pre>
Line 3,150 ⟶ 4,068:
United Kingdom GB 22 GB29NWBK60161331926819
</pre>
<syntaxhighlight lang="powershell">
$regex = [regex]'[a-zA-Z]{2}[0-9]{2}[a-zA-Z0-9]{6}[0-9]{5}([a-zA-Z0-9]?){0,16}'
Line 3,161 ⟶ 4,079:
}
}
</syntaxhighlight>
{{Out}}
<pre>
Line 3,216 ⟶ 4,134:
=={{header|PureBasic}}==
<
Enumeration IBAN
#IBAN_VAL
Line 3,307 ⟶ 4,225:
ForEver
Input()
End</
{{out}}
<pre>
Line 3,326 ⟶ 4,244:
=={{header|Python}}==
{{trans|Ruby}}
<
_country2length = dict(
Line 3,353 ⟶ 4,271:
if __name__ == '__main__':
for account in ["GB82 WEST 1234 5698 7654 32", "GB82 TEST 1234 5698 7654 32"]:
print('%s validation is: %s' % (account, valid_iban(account)))</
GB82 WEST 1234 5698 7654 32 validation is: True
GB82 TEST 1234 5698 7654 32 validation is: False
=={{header|Racket}}==
<
(define lens
'([AL 28] [AD 24] [AT 20] [AZ 28] [BH 22] [BE 16] [BA 20] [BR 29] [BG 22]
Line 3,382 ⟶ 4,300:
(valid-iban? "GB82 WEST 1234 5698 7654 32") ; => #t
(valid-iban? "GB82 TEST 1234 5698 7654 32") ; => #f</
=={{header|Raku}}==
(formerly Perl 6)
<syntaxhighlight lang="raku" line>subset IBAN of Str where sub ($_ is copy) {
s:g/\s//;
return False if m/<-alnum>/ or .chars != <
AD 24 AE 23 AL 28 AT 20 AZ 28 BA 20 BE 16 BG 22 BH 22 BR 29 CH 21
CR 21 CY 28 CZ 24 DE 22 DK 18 DO 28 EE 20 ES 24 FI 18 FO 18 FR 27
GB 22 GE 22 GI 23 GL 18 GR 27 GT 28 HR 21 HU 28 IE 22 IL 23 IS 26
IT 27 KW 30 KZ 20 LB 28 LI 21 LT 20 LU 20 LV 21 MC 27 MD 24 ME 22
MK 19 MR 27 MT 31 MU 30 NL 18 NO 15 PK 24 PL 28 PS 29 PT 25 RO 24
RS 22 SA 24 SE 24 SI 19 SK 24 SM 27 TN 24 TR 26 VG 24
>.hash{.substr(0,2).uc};
s/(.**4)(.+)/$1$0/;
return .subst(:g, /\D/, { :36(~$_) }) % 97 == 1;
}
say "$_ is {$_ ~~ IBAN ?? 'valid' !! 'invalid' }" for
'GB82 WEST 1234 5698 7654 32',
'gb82 west 1234 5698 7654 32',
'GB82 TEST 1234 5698 7654 32';</syntaxhighlight>{{out}}
GB82 WEST 1234 5698 7654 32 is valid.
gb82 west 1234 5698 7654 32 is valid.
GB82 TEST 1234 5698 7654 32 is invalid.
=={{header|REXX}}==
These REXX programs can validate an IBAN specified on the command line or from an internal list.
===basic checking===
<
@.=; @.1 = 'GB82 WEST 1234 5698 7654 32 '
@.2 = 'Gb82 West 1234 5698 7654 32 '
Line 3,431 ⟶ 4,374:
if z//97==1 then return 0 /*check if correct remainder (modulus).*/
return e 'check digits.'</
{{out|output|text= when using the default input:}}
<pre>
Line 3,451 ⟶ 4,394:
:* checks for two countries that may not be valid (as per their entry date into the IBAN system)
:* checks some countries to make sure their check digits match a specific value
<
@.=; @.1 = 'GB82 WEST 1234 5698 7654 32 '
@.2 = 'Gb82 West 1234 5698 7654 32 '
Line 3,504 ⟶ 4,447:
if z//97==1 then return 0 /*check if correct remainder (modulus).*/
return e 'check digits.'</
{{out|output|text= when using the default input, (the run date of this program is 29-April-2013):}}
<pre>
Line 3,523 ⟶ 4,466:
=={{header|Ring}}==
<
# Project : IBAN
codes = list(5)
Line 3,579 ⟶ 4,519:
modnew = number(modnew) % 97
return modnew
</syntaxhighlight>
Output:
<pre>
Line 3,591 ⟶ 4,531:
=={{header|Ruby}}==
{{works with|Ruby|1.9+}}
<
len = {
AL: 28, AD: 24, AT: 20, AZ: 28, BE: 16, BH: 22, BA: 20, BR: 29,
Line 3,619 ⟶ 4,559:
p valid_iban? "GB82 WEST 1234 5698 7654 32" #=> true
p valid_iban? "GB82 TEST 1234 5698 7654 32" #=> false</
=={{header|Rust}}==
<syntaxhighlight lang="rust">
fn main() {
for iban in [
Line 3,688 ⟶ 4,628:
}) == 1
}
</syntaxhighlight>
=={{header|Scala}}==
{{libheader|Scala}}<
class Iban(val iban: String) {
Line 3,732 ⟶ 4,672:
map(v => (v.take(2), if (v.isEmpty) 0 else v.slice(2, 4).toInt))
lazy val pattern = "([A-Z]{2})([0-9]{2
def apply(s: String) = new Iban(s.replaceAll( """\s""", ""))
}</
<
def blackCases = """AT611904300235473201
|GB82TEST12345698765432
Line 3,833 ⟶ 4,773:
blackCases.lines.foreach(l => assert(!Iban(l).isValidIban))
println(s"Successfully completed; ${whiteCases.lines.size + blackCases.lines.size} cases tested, no errors.")
}</
{{out}}
Successfully completed; 91 cases tested, no errors.
=={{header|Seed7}}==
<
include "bigint.s7i";
Line 3,894 ⟶ 4,834:
check("GB82 TEST 1234 5698 7654 32");
check("GB82 WEST 1243 5698 7654 32");
end func;</
{{out}}
<pre>
Line 3,905 ⟶ 4,845:
=={{header|Sidef}}==
<
static len = Hash(
AD=>24, AE=>23, AL=>28, AO=>25, AT=>20, AZ=>28, BA=>20, BE=>16, BF=>27,
Line 3,935 ⟶ 4,875:
say valid_iban("GB82 WEST 1234 5698 7654 32") #=> true
say valid_iban("GB82 TEST 1234 5698 7654 32") #=> false</
=={{header|SNOBOL4}}==
<
DEFINE('ibantable()') :(iban_table_end)
ibantable
Line 4,018 ⟶ 4,958:
OUTPUT = "valid IBAN: " line :(read)
END</
'''Output:'''
Line 4,034 ⟶ 4,974:
Invalid IBAN: IL62-0108-0000-0009-9999-999: length: 28 not 23
Invalid IBAN: GR16 0110 1250 0000 0001 2300 695X: length: 28 not 27</pre>
=={{header|Standard ML}}==
{{works with|SML/NJ}}
<
(* Get the length of a valid IBAN given the two chars long country code *)
fun country_code (str : string) : int =
Line 4,106 ⟶ 5,044:
IntInf.mod (number, 97) = 1
end
end handle Subscript => false | Domain => false</
{{output}}
Line 4,115 ⟶ 5,053:
=={{header|Tcl}}==
<
# Normalize by up-casing and stripping illegal chars (e.g., space)
set iban [regsub -all {[^A-Z0-9]+} [string toupper $iban] ""]
Line 4,148 ⟶ 5,086:
# Verify length and modulus
return [expr {[string length $iban] == $len && $num % 97 == 1}]
}</
<
puts "$iban is [expr {[verifyIBAN $iban] ? {verified} : {unverified}}]"
set not "GB42 WEST 1234 5698 7654 32"
puts "$not is [expr {[verifyIBAN $not] ? {verified} : {unverified}}]"</
<pre>GB82 WEST 1234 5698 7654 32 is verified
GB42 WEST 1234 5698 7654 32 is unverified</pre>
Line 4,159 ⟶ 5,097:
{{works with|bash}}
This does not verify the country code or the length.
<
[A]=10 [B]=11 [C]=12 [D]=13 [E]=14 [F]=15 [G]=16 [H]=17 [I]=18
[J]=19 [K]=20 [L]=21 [M]=22 [N]=23 [O]=24 [P]=25 [Q]=26 [R]=27
Line 4,192 ⟶ 5,130:
printf "%s : " "$test"
is_iban "$test" && echo yes || echo no
done</
{{output}}
<pre>GB82 WEST 1234 5698 7654 32 : yes
GB42 WEST 1234 5698 7654 32 : no</pre>
=={{header|VBA}}==
{{trans|Phix}}<syntaxhighlight lang="vb">Public nations As New Collection
Private Sub init()
nations.Add 24, "AD"
nations.Add 21, "CH"
nations.Add 22, "GB"
nations.Add 24, "SA"
nations.Add 20, "XK"
End Sub
Private Function mod97(ByVal c As String) As Integer
Dim n As Long
n = Val(Mid(c, 1, 9))
c = Mid(c, 10, Len(c) - 9)
n = n Mod 97
Do While Len(c) > 6
n = Val(Str(n) & Mid(c, 1, 7))
n = n Mod 97
c = Mid(c, 8, Len(c) - 7)
Loop
n = Val(Str(n) & c)
mod97 = n Mod 97
End Function
Private Function iban(ByVal code As String) As Boolean
'-- This routine does and should reject codes containing spaces etc.
'-- Use iban_s() below for otherwise.
On Error GoTo 1
lengths = nations(Mid(code, 1, 2))
If Len(code) = lengths Then
code = code & Mid(code, 1, 4)
code = Mid(code, 5, lengths)
Dim c As String: c = ""
Dim ch As String
For i = 1 To lengths
ch = Mid(code, i, 1)
If ch >= "0" And ch <= "9" Then
c = c & ch
Else
If ch >= "A" And ch <= "Z" Then
c = c & Str(Asc(ch) - 55)
Else
iban = False
End If
End If
Next i
c = Replace(c, " ", "")
iban = mod97(c) = 1
End If
Exit Function
1:
iban = False
End Function
Private Function iban_s(code As String) As Boolean
'-- strips any embedded spaces and hyphens before validating.
code = Replace(code, " ", "")
code = Replace(code, "-", "")
iban_s = iban(code)
End Function
Private Sub test(code As String, expected As Boolean)
Dim valid As Boolean
valid = iban_s(code)
Dim state As String
If valid = expected Then
state = IIf(valid, "ok", "invalid (as expected)")
Else
state = IIf(valid, "OK!!", "INVALID!!") & " (NOT AS EXPECTED)"
End If
Debug.Print code, state
End Sub
Public Sub main()
init
test "GB82 WEST 1234 5698 7654 32", True
test "GB82 TEST 1234 5698 7654 32", False
test "GB81 WEST 1234 5698 7654 32", False
test "SA03 8000 0000 6080 1016 7519", True
test "CH93 0076 2011 6238 5295 7", True
End Sub</syntaxhighlight>{{out}}
<pre>GB82WEST12345698765432 ok
GB82TEST12345698765432 invalid (as expected)
GB81WEST12345698765432 invalid (as expected)
SA0380000000608010167519 ok
CH9300762011623852957 ok</pre>
=={{header|VBScript}}==
<syntaxhighlight lang="vb">
Function validate_iban(s)
validate_iban = Chr(34) & s & Chr(34) & " is NOT valid."
Line 4,258 ⟶ 5,283:
WScript.StdOut.WriteLine validate_iban("US12 3456 7890 0987 6543 210")
WScript.StdOut.WriteLine validate_iban("GR16 0110 1250 0000 0001 2300 695X")
</syntaxhighlight>
{{Out}}
Line 4,275 ⟶ 5,300:
"GR16 0110 1250 0000 0001 2300 695X" is NOT valid.
</pre>
=={{header|Wren}}==
{{trans|Kotlin}}
{{libheader|Wren-big}}
<syntaxhighlight lang="wren">import "./big" for BigInt
var countryCodes =
"AD24 AE23 AL28 AT20 AZ28 BA20 BE16 BG22 BH22 BR29 " +
"BY28 CH21 CR22 CY28 CZ24 DE22 DK18 DO28 EE20 ES24 " +
"FI18 FO18 FR27 GB22 GE22 GI23 GL18 GR27 GT28 HR21 " +
"HU28 IE22 IL23 IQ23 IS26 IT27 JO30 KW30 KZ20 LB28 " +
"LC32 LI21 LT20 LU20 LV21 MC27 MD24 ME22 MK19 MR27 " +
"MT31 MU30 NL18 NO15 PK24 PL28 PS29 PT25 QA29 RO24 " +
"RS22 SA24 SC31 SE24 SI19 SK24 SM27 ST25 SV28 TL23 " +
"TN24 TR26 UA29 VG24 XK20"
var isValid = Fn.new { |iban|
// remove spaces from IBAN
var s = iban.replace(" ", "")
// check country code and length
if (!countryCodes.contains(s[0..1] + s.count.toString)) return false
// move first 4 characters to the end
s = s[4..-1] + s[0..3]
// replace A to Z with numbers 10 To 35
var i = 10
for (c in "ABCDEFGHIJKLMNOPQRSTUVWXYZ") {
s = s.replace(c, i.toString)
i = i + 1
}
// check whether mod 97 calculation gives a remainder of 1
return (BigInt.new(s) % BigInt.new(97)) == BigInt.one
}
var ibans = [ "GB82 WEST 1234 5698 7654 32", "GB82 TEST 1234 5698 7654 32" ]
for (iban in ibans) {
var valid = isValid.call(iban)
System.print(iban + (valid ? " may be valid" : " is not valid"))
}</syntaxhighlight>
{{out}}
<pre>
GB82 WEST 1234 5698 7654 32 may be valid
GB82 TEST 1234 5698 7654 32 is not valid
</pre>
=={{header|Yabasic}}==
{{trans|Phix}}
<syntaxhighlight lang="yabasic">// List updated to release 72, 25 November 2016, of IBAN Registry (75 countries)
countryCodes$ = "AD24 AE23 AL28 AT20 AZ28 BA20 BE16 BG22 BH22 BR29 BY28 CH21 CR22 CY28 CZ24 DE22 "
countryCodes$ = countryCodes$ + "DK18 DO28 EE20 ES24 FI18 FO18 FR27 GB22 GE22 GI23 GL18 GR27 GT28 HR21 HU28 IE22 "
countryCodes$ = countryCodes$ + "IL23 IQ23 IS26 IT27 JO30 KW30 KZ20 LB28 LC32 LI21 LT20 LU20 LV21 MC27 MD24 ME22 "
countryCodes$ = countryCodes$ + "MK19 MR27 MT31 MU30 NL18 NO15 PK24 PL28 PS29 PT25 QA29 RO24 RS22 SA24 SC31 SE24 "
countryCodes$ = countryCodes$ + "SI19 SK24 SM27 ST25 SV28 TL23 TN24 TR26 UA29 VG24 XK20"
sub iban(code$)
// This routine does and should reject codes containing spaces etc.
// Use iban_s() below for otherwise.
local country, lcode, c, i, ch$
lcode = len(code$)
country = instr(countryCodes$, upper$(left$(code$, 2)))
if country and lcode = val(mid$(countryCodes$, country + 2, 2)) then
code$ = right$(code$, lcode - 4) + left$(code$, 4)
for i = 1 to lcode
ch$ = mid$(code$, i, 1)
if ch$ >= "0" and ch$ <= "9" then
c = c * 10 + asc(ch$) - asc("0")
elsif ch$ >= "A" and ch$ <= "Z" then
c = c * 100 + asc(ch$) - 55
else
return false
end if
c = mod(c, 97)
next i
return c = 1
end if
return false
end sub
sub iban_s(code$)
// strips any embedded spaces and hyphens before validating.
local i, t$(1), n
i = token(code$, t$(), " -")
code$ = ""
for n = 1 to i
code$ = code$ + t$(n)
next n
return iban(code$)
end sub
sub test(code$, expected)
local valid, state$
valid = iban_s(code$)
if valid = expected then
if valid then
state$ = "ok"
else
state$ = "invalid (as expected)"
end if
else
if valid then
state$ = "OK!!"
else
state$ = "INVALID!!"
end if
state$ = state$ + " (NOT AS EXPECTED)"
end if
print code$, "\t ", state$
end sub
test("GB82 WEST 1234 5698 7654 32", true)
test("GB82 TEST 1234 5698 7654 32", false)
test("GB81 WEST 1234 5698 7654 32", false)
test("SA03 8000 0000 6080 1016 7519", true)
test("CH93 0076 2011 6238 5295 7", true)</syntaxhighlight>
=={{header|zkl}}==
Using GMP big nums:
<
fcn validateIBAN(iban){
iban=iban-" \t";
alphaNums.matches(iban) and (ibans.find(iban[0,2])==iban.len()) and
( BN((iban[4,*]+iban[0,4]).apply("toInt",36)) % 97 == 1 )
}</
<
iban=iban-" \t";
alphaNums.matches(iban) and (ibans.find(iban[0,2])==iban.len()) and
Line 4,295 ⟶ 5,444:
}
M
}</
var ibans= // Dictionary("AD":24, ...)
("AD24 AE23 AL28 AO25 AT20 AZ28 BA20 BE16 BF27 BG22 BH22 BI16 "
Line 4,304 ⟶ 5,453:
"MR27 MT31 MU30 MZ25 NL18 NO15 PK24 PL28 PS29 PT25 QA29 RO24 "
"RS22 SA24 SE24 SI19 SK24 SM27 SN28 TN24 TR26 UA29 VG24")
.split(" ").pump(D(),fcn(w){return(w[0,2],w[2,*].toInt())});</
<
T("GB82 WEST 1234 5698 7654 32","GB82WEST12345698765432",
"GR16 0110 1250 0000 0001 2300 695","GB29 NWBK 6016 1331 9268 19",
Line 4,324 ⟶ 5,473:
...
"CH9300762011623852957").split()
.apply(validateIBAN).toString(*).println();</
{{out}}
<pre>
Line 4,340 ⟶ 5,489:
=={{header|ZX Spectrum Basic}}==
{{trans|BBC BASIC}}
<
20 REM REM http://www.cnb.cz/cs/platebni_styk/iban/download/EBS204.pdf
30 REM REM Pairs of ISO 3166 country code & expected IBAN length for this country
Line 4,403 ⟶ 5,552:
4050 NEXT i
4060 RETURN
5000 DEF FN m(a,b)=a-INT (a/b)*b: REM modulo</
|