IBAN: Difference between revisions
m
→{{header|Wren}}: Minor tidy
m (→{{header|Wren}}: Minor tidy) |
|||
(5 intermediate revisions by 5 users not shown) | |||
Line 24:
{{trans|Python}}
<
V segstart = 0
V step = 9
Line 62:
L(account) [‘GB82 WEST 1234 5698 7654 32’,
‘GB82 TEST 1234 5698 7654 32’]
print(‘#. validation is: #.’.format(account, valid_iban(account)))</
{{out}}
Line 71:
=={{header|Ada}}==
<
function Is_Legal(Iban : String) return Boolean;
end Iban_Code;</
with Ada.Containers.Hashed_Maps;
with Ada.Strings.Hash;
Line 160:
Nations.insert("TR", 26); Nations.insert("AE", 23);
Nations.insert("GB", 22); Nations.insert("VG", 24);
end Iban_Code;</
<
with Iban_Code;
Line 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 190:
=={{header|AppleScript}}==
<
-- A list of 34 lists. The nth list (1-indexed) contains country codes for countries having n-character IBANS.
return {{}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {"NO"}, {"BE"}, ¬
Line 251:
set output to output as text
set AppleScript's text item delimiters to astid
return output</
{{output}}
<
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"</
=={{header|Arturo}}==
<
AL: 28, AD: 24, AT: 20, AZ: 28, BE: 16
BH: 22, BA: 20, BR: 29, BG: 22, CR: 21, HR: 21, CY: 28
Line 293:
loop ["GB82 WEST 1234 5698 7654 32"
"GB82 TEST 1234 5698 7654 32"] 'ib
-> print [ib "=> valid?" valid? ib]</
{{out}}
Line 302:
=={{header|AutoHotkey}}==
{{works with|AutoHotkey 1.1}}
<
, "gb82 WEST 1234 5698 7654 32"
, "GB82WEST12345698765432"
Line 344:
}
return rem
}</
GB82 WEST 1234 5698 7654 32 is valid.
gb82 WEST 1234 5698 7654 32 is valid.
Line 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 407:
ibancsum % 97 == 1 ? valid() : invalid()
}</
Creating a test file and launching the script:
<syntaxhighlight lang="text">
cat > test.iban
FR33 ^__^ 0BAD
Line 419:
^D
gawk -Mf iban.gawk test.iban
</syntaxhighlight>
Output:
<syntaxhighlight lang="text">
INVALID FR33 ^__^ 0BAD
INVALID AA11 1234 6543 1212
Line 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 526:
num$=MID$(num$,8)
ENDWHILE
=VALmod$</
valid IBAN: GB82 WEST 1234 5698 7654 32
valid IBAN: GB82WEST12345698765432
Line 542:
=={{header|Befunge}}==
<
<<_#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>>
Line 549:
"-(&(/$$*$(*.-*.('$)*%0-&**.$'(.'/.+,''&&*.)**&,-.&.(*(*-!(%01-)
BFBBBBFFTNRRGGCCGCGGSSKSKSSDDHDHCPLPTLLLLPPAAVEIIAAAIEMMMNGMMMMI
ERGAHRIONLSOTRZYBRLIKIWMZAEOKREUHSBTRIVTUKLEDGESTLTZESEDCOEKUTRL</
{{out}}
Line 560:
=={{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 610:
& IBAN-check$"GB82 WEST 1243 5698 7654 32"
& IBAN-check$"GB82 west 1243 5698 7654 32"
);</
wrong length
invalid country code: 'GX'
Line 621:
=={{header|C}}==
<
#include <ctype.h>
#include <stdio.h>
Line 717:
return 0;
}</
iban 'GB82 WEST 1234 5698 7654 32' GB82TEST12345698765432
GB82WEST12345698765432 is valid.
Line 723:
=={{header|C sharp}}==
<
{
public ValidationResult Validate(string value)
Line 841:
{"VG", 24}
};
}</
<
{
[Fact]
Line 941:
Assert.Equal(ValidationResult.IsValid, result);
}
}</
=={{header|C++}}==
<
#include <iostream>
#include <boost/algorithm/string.hpp>
Line 1,018:
SayValidity("GB82TEST12345698765432");
return 0;
}</
GB82 WEST 1234 5698 7654 32 is valid!
GB82TEST12345698765432 is not valid!
Line 1,024:
=={{header|Caché ObjectScript}}==
<
{
Line 1,121:
}
}</
<pre>USER>For { Read iban Quit:iban="" Write " => ", ##class(Utils.Validate).VerifyIBAN(iban), ! }
GB82 WEST 1234 5698 7654 32 => 1
Line 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 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,321:
END PROGRAM create-iban-number.
END PROGRAM validate-iban.</
<pre>
GB82 WEST 1234 5698 7654 32 is valid.
Line 1,328:
=={{header|Common Lisp}}==
<
;;
;; List of the IBAN code lengths per country.
Line 1,390:
(= 1 (mod (IBAN-to-integer X) 97)))))
(validp (remove #\Space iban))))
</syntaxhighlight>
'''Output:'''
<pre>
Line 1,403:
=={{header|D}}==
{{trans|Python}}
<
std.algorithm, std.ascii;
Line 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.
;
Line 1,599:
ELSE ISVALID=
XRETURN
END</
{{out}}
<pre>GB82 WEST 1234 5698 7654 32 is valid
Line 1,613:
=={{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,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,661:
=={{header|F_Sharp|F#}}==
<
open System.Text.RegularExpressions
Line 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,720:
=={{header|Factor}}==
<
math.parser regexp sequences sets qw unicode ;
IN: rosetta-code.iban
Line 1,769:
] bi@ ;
MAIN: iban-demo</
{{out}}
<pre>
Line 1,778:
=={{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,815:
;
checkiban</
{{Out}}
<pre>linux:~> pp4th -x chkiban.4th
Line 1,825:
=={{header|Fortran}}==
<
program ibancheck
Line 1,950:
end program ibancheck
</syntaxhighlight>
{{out}}
Line 1,969:
=={{header|FreeBASIC}}==
<
' List updated to release 72, 25 November 2016, of IBAN Registry (75 countries)
Line 2,040:
Print
Print "Press any key to quit"
Sleep</
{{out}}
Line 2,050:
=={{header|Free Pascal}}==
{{libheader|GMP}}
<
uses
Line 2,130:
// note, strings longer than 34 characters will be silently clipped
writeLn(isLegal(delSpace('GB82 WEST 1234 5698 7654 32')));
end.</
{{out}}
<pre>TRUE</pre>
Line 2,136:
=={{header|Go}}==
<
import (
Line 2,205:
}
}
</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 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 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 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 2,294:
invalidBecause reason = Left $ "Invalid IBAN number " ++ xs ++
": " ++ reason</
<pre>
validateIBAN "GB82 WEST 1234 5698 7654 32"
Line 2,316:
=={{header|IS-BASIC}}==
<
110 STRING CO$(1 TO 93)*2
120 NUMERIC LG(1 TO 93)
Line 2,373:
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</
=={{header|J}}==
<
delblk =. #~ ' '&~:
NB. rearrange
Line 2,410:
NB. IBAN
iban =: quads @: ibancheck
</
<pre>
iban 'GB82 WEST 1234 5698 7654 32'
Line 2,425:
=={{header|Java}}==
{{works with|Java|8+}}
<
import java.util.*;
Line 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 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 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 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 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,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,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.
<
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 2,617:
isIBAN('SA03 8000 0000 6080 1016 7519') ==> true
=!EXPECTEND!=
*/</
{{out}}
Line 2,626:
{{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,647:
digs = parse(BigInt, join(parse(Int, ch, 36) for ch in iban))
return rst && digs % 97 == 1
end</
{{out}}
Line 2,654:
=={{header|Kotlin}}==
<
import java.math.BigInteger
Line 2,697:
println(iban + if (valid) " may be valid" else " is not valid")
}
}</
{{out}}
Line 2,703:
GB82 WEST 1234 5698 7654 32 may be valid
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",
Line 2,766 ⟶ 2,895:
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,899 ⟶ 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,930 ⟶ 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$ {
Line 2,977 ⟶ 3,106:
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 3,008 ⟶ 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 3,021 ⟶ 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 3,050 ⟶ 3,179:
i2 = min(i1+8, nDig);
end
end</
<
'GB82 TEST 1234 5698 7654 32' ;
'CH93 0076 2011 6238 5295 7' ;
Line 3,061 ⟶ 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 3,072 ⟶ 3,201:
=={{header|NewLISP}}==
<syntaxhighlight lang="newlisp">
(setq *iban-code-length* '((15 ("NO"))
(16 ("BE"))
Line 3,140 ⟶ 3,269:
)
)
</syntaxhighlight>
Output:
<pre>
Line 3,152 ⟶ 3,281:
=={{header|Nim}}==
{{libheader|bigints}}
<
let countryLen = toTable({
Line 3,185 ⟶ 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 3,191 ⟶ 3,320:
=={{header|Oberon-2}}==
Works with oo2c Version 2
<
MODULE IBAN;
IMPORT
Line 3,353 ⟶ 3,482:
Test;
END IBAN.
</syntaxhighlight>
Output:
<pre>
Line 3,364 ⟶ 3,493:
=={{header|OCaml}}==
{{works with|OCaml|4.03+}}
<
#load "str.cma"
#load "nums.cma" (* for module Big_int *)
Line 3,471 ⟶ 3,600:
in
List.iter (fun pair -> testit pair) ibans
</syntaxhighlight>
{{out}}
GB82 WEST 1234 5698 7654 32 is valid. Expected true [PASS]
Line 3,482 ⟶ 3,611:
=={{header|Perl}}==
<
use strict ;
use warnings ;
Line 3,529 ⟶ 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}}==
<!--<
<span style="color: #000080;font-style:italic;">--
-- demo\rosetta\IBAN.exw
Line 3,594 ⟶ 3,723:
<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>
<!--</
{{out}}
<pre>
Line 3,605 ⟶ 3,734:
=={{header|PHP}}==
<
<?php
Line 3,685 ⟶ 3,814:
echo "IBAN NOT VALID";
}
</syntaxhighlight>
=={{header|Picat}}==
<
IBAN = "GB82 WEST 1234 5698 7654 32",
println(IBAN=iban(IBAN)),
Line 3,721 ⟶ 3,850:
"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]).</
{{out}}
<pre>GB82 WEST 1234 5698 7654 32 = ok</pre>
===Some more (and interesting) tests
<
Ibans = ["GB82 WEST 1234 5698 7654 32",
"gb82 WEST 1234 5698 7654 32",
Line 3,745 ⟶ 3,874:
println(IBAN=iban(IBAN))
end,
nl.</
{{out}}
<pre>GB82 WEST 1234 5698 7654 32 = ok
gb82 WEST 1234 5698 7654 32 = ok
Line 3,760 ⟶ 3,889:
CH93 0076 2011 6238 5295 7 = ok
IL62 0108 0000 0009 9999 999 = ok</pre>
=={{header|PicoLisp}}==
<
(de iban (Str)
Line 3,792 ⟶ 3,920:
(println 'Invalid) ) )
(bye)</
=={{header|PowerShell}}==
<syntaxhighlight lang="powershell">
function verifIBAN ([string]$ibanS)
{
Line 3,822 ⟶ 3,950:
}
} #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,887 ⟶ 4,015:
$ibans
</syntaxhighlight>
{{Out}}
<pre>
Line 3,940 ⟶ 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,951 ⟶ 4,079:
}
}
</syntaxhighlight>
{{Out}}
<pre>
Line 4,006 ⟶ 4,134:
=={{header|PureBasic}}==
<
Enumeration IBAN
#IBAN_VAL
Line 4,097 ⟶ 4,225:
ForEver
Input()
End</
{{out}}
<pre>
Line 4,116 ⟶ 4,244:
=={{header|Python}}==
{{trans|Ruby}}
<
_country2length = dict(
Line 4,143 ⟶ 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 4,172 ⟶ 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"
s:g/\s//;
return False if m/<-
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
Line 4,194 ⟶ 4,322:
'GB82 WEST 1234 5698 7654 32',
'gb82 west 1234 5698 7654 32',
'GB82 TEST 1234 5698 7654 32';</
GB82 WEST 1234 5698 7654 32 is valid.
gb82 west 1234 5698 7654 32 is valid.
Line 4,202 ⟶ 4,330:
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 4,246 ⟶ 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 4,266 ⟶ 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 4,319 ⟶ 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 4,338 ⟶ 4,466:
=={{header|Ring}}==
<
# Project : IBAN
Line 4,391 ⟶ 4,519:
modnew = number(modnew) % 97
return modnew
</syntaxhighlight>
Output:
<pre>
Line 4,403 ⟶ 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 4,431 ⟶ 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 4,500 ⟶ 4,628:
}) == 1
}
</syntaxhighlight>
=={{header|Scala}}==
{{libheader|Scala}}<
class Iban(val iban: String) {
Line 4,547 ⟶ 4,675:
def apply(s: String) = new Iban(s.replaceAll( """\s""", ""))
}</
<
def blackCases = """AT611904300235473201
|GB82TEST12345698765432
Line 4,645 ⟶ 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 4,706 ⟶ 4,834:
check("GB82 TEST 1234 5698 7654 32");
check("GB82 WEST 1243 5698 7654 32");
end func;</
{{out}}
<pre>
Line 4,717 ⟶ 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 4,747 ⟶ 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,830 ⟶ 4,958:
OUTPUT = "valid IBAN: " line :(read)
END</
'''Output:'''
Line 4,850 ⟶ 4,978:
{{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,916 ⟶ 5,044:
IntInf.mod (number, 97) = 1
end
end handle Subscript => false | Domain => false</
{{output}}
Line 4,925 ⟶ 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,958 ⟶ 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,969 ⟶ 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 5,002 ⟶ 5,130:
printf "%s : " "$test"
is_iban "$test" && echo yes || echo no
done</
{{output}}
Line 5,009 ⟶ 5,137:
=={{header|VBA}}==
{{trans|Phix}}<
Private Sub init()
nations.Add 24, "AD"
Line 5,088 ⟶ 5,216:
test "SA03 8000 0000 6080 1016 7519", True
test "CH93 0076 2011 6238 5295 7", True
End Sub</
<pre>GB82WEST12345698765432 ok
GB82TEST12345698765432 invalid (as expected)
Line 5,096 ⟶ 5,224:
=={{header|VBScript}}==
<syntaxhighlight lang="vb">
Function validate_iban(s)
validate_iban = Chr(34) & s & Chr(34) & " is NOT valid."
Line 5,155 ⟶ 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 5,176 ⟶ 5,304:
{{trans|Kotlin}}
{{libheader|Wren-big}}
<
var countryCodes =
Line 5,213 ⟶ 5,341:
var valid = isValid.call(iban)
System.print(iban + (valid ? " may be valid" : " is not valid"))
}</
{{out}}
Line 5,223 ⟶ 5,351:
=={{header|Yabasic}}==
{{trans|Phix}}
<
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 "
Line 5,295 ⟶ 5,423:
test("GB81 WEST 1234 5698 7654 32", false)
test("SA03 8000 0000 6080 1016 7519", true)
test("CH93 0076 2011 6238 5295 7", true)</
=={{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 5,316 ⟶ 5,444:
}
M
}</
var ibans= // Dictionary("AD":24, ...)
("AD24 AE23 AL28 AO25 AT20 AZ28 BA20 BE16 BF27 BG22 BH22 BI16 "
Line 5,325 ⟶ 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 5,345 ⟶ 5,473:
...
"CH9300762011623852957").split()
.apply(validateIBAN).toString(*).println();</
{{out}}
<pre>
Line 5,361 ⟶ 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 5,424 ⟶ 5,552:
4050 NEXT i
4060 RETURN
5000 DEF FN m(a,b)=a-INT (a/b)*b: REM modulo</
|