Validate International Securities Identification Number: Difference between revisions

Content added Content deleted
m (syntax highlighting fixup automation)
Line 68: Line 68:
{{trans|Python}}
{{trans|Python}}


<lang 11l>F check_isin(a)
<syntaxhighlight lang="11l">F check_isin(a)
I a.len != 12
I a.len != 12
R 0B
R 0B
Line 91: Line 91:


print([‘US0378331005’, ‘US0373831005’, ‘U50378331005’, ‘US03378331005’,
print([‘US0378331005’, ‘US0373831005’, ‘U50378331005’, ‘US03378331005’,
‘AU0000XVGZA3’, ‘AU0000VXGZA3’, ‘FR0000988040’].map(s -> check_isin(s)))</lang>
‘AU0000XVGZA3’, ‘AU0000VXGZA3’, ‘FR0000988040’].map(s -> check_isin(s)))</syntaxhighlight>


{{out}}
{{out}}
Line 99: Line 99:


=={{header|360 Assembly}}==
=={{header|360 Assembly}}==
<lang 360asm>* Validate ISIN 08/03/2019
<syntaxhighlight lang="360asm">* Validate ISIN 08/03/2019
VALISIN CSECT
VALISIN CSECT
USING VALISIN,R13 base register
USING VALISIN,R13 base register
Line 274: Line 274:
XDEC DS CL12 temp for xdeco and xdeci
XDEC DS CL12 temp for xdeco and xdeci
REGEQU
REGEQU
END VALISIN</lang>
END VALISIN</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 289: Line 289:
Calling the existing Luhn algorithm implementation from the ''[[Luhn test of credit card numbers]]'' task.
Calling the existing Luhn algorithm implementation from the ''[[Luhn test of credit card numbers]]'' task.


<lang Ada>procedure ISIN is
<syntaxhighlight lang="ada">procedure ISIN is
-- Luhn_Test copied from other Task
-- Luhn_Test copied from other Task
function Luhn_Test (Number: String) return Boolean is
function Luhn_Test (Number: String) return Boolean is
Line 362: Line 362:
when others =>
when others =>
Ada.Text_IO.Put_Line("Exception occured");
Ada.Text_IO.Put_Line("Exception occured");
end ISIN;</lang>
end ISIN;</syntaxhighlight>


Output:
Output:
Line 375: Line 375:
=={{header|ALGOL W}}==
=={{header|ALGOL W}}==
Uses the LuhnTest procedure from the [[Luhn test of credit card numbers]] task.
Uses the LuhnTest procedure from the [[Luhn test of credit card numbers]] task.
<lang algolw>begin
<syntaxhighlight lang="algolw">begin
% external procedure that returns true if ccNumber passes the Luhn test, false otherwise %
% external procedure that returns true if ccNumber passes the Luhn test, false otherwise %
logical procedure LuhnTest ( string(32) value ccNumber
logical procedure LuhnTest ( string(32) value ccNumber
Line 449: Line 449:
testIsIsin( "AU0000VXGZA3", true );
testIsIsin( "AU0000VXGZA3", true );
testIsIsin( "FR0000988040", true );
testIsIsin( "FR0000988040", true );
end.</lang>
end.</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 465: Line 465:
This script calls a handler posted for the [https://www.rosettacode.org/wiki/Luhn_test_of_credit_card_numbers#Straightforward Luhn test of credit card numbers] task.
This script calls a handler posted for the [https://www.rosettacode.org/wiki/Luhn_test_of_credit_card_numbers#Straightforward Luhn test of credit card numbers] task.


<lang applescript>use AppleScript version "2.4" -- OS X 10.10 (Yosemite) or later
<syntaxhighlight lang="applescript">use AppleScript version "2.4" -- OS X 10.10 (Yosemite) or later
use framework "Foundation"
use framework "Foundation"


Line 490: Line 490:
set end of testResults to {testNumber:ISIN's contents, valid:ISINTest(ISIN)}
set end of testResults to {testNumber:ISIN's contents, valid:ISINTest(ISIN)}
end repeat
end repeat
return testResults</lang>
return testResults</syntaxhighlight>


{{output}}
{{output}}
<lang applescript>{{testNumber:"US0378331005", valid:true}, {testNumber:"US0373831005", valid:false}, {testNumber:"U50378331005", valid:false}, {testNumber:"US03378331005", valid:false}, {testNumber:"AU0000XVGZA3", valid:true}, {testNumber:"AU0000VXGZA3", valid:true}, {testNumber:"FR0000988040", valid:true}}</lang>
<syntaxhighlight lang="applescript">{{testNumber:"US0378331005", valid:true}, {testNumber:"US0373831005", valid:false}, {testNumber:"U50378331005", valid:false}, {testNumber:"US03378331005", valid:false}, {testNumber:"AU0000XVGZA3", valid:true}, {testNumber:"AU0000VXGZA3", valid:true}, {testNumber:"FR0000988040", valid:true}}</syntaxhighlight>
=={{header|AWK}}==
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f VALIDATE_INTERNATIONAL_SECURITIES_IDENTIFICATION_NUMBER.AWK
# syntax: GAWK -f VALIDATE_INTERNATIONAL_SECURITIES_IDENTIFICATION_NUMBER.AWK
# converted from Fortran
# converted from Fortran
Line 532: Line 532:
return(v % 10 == 0)
return(v % 10 == 0)
}
}
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 545: Line 545:


=={{header|C}}==
=={{header|C}}==
<lang c>#include <stdio.h>
<syntaxhighlight lang="c">#include <stdio.h>


int check_isin(char *a) {
int check_isin(char *a) {
Line 590: Line 590:
}
}


/* will print: T F F F T T T */</lang>
/* will print: T F F F T T T */</syntaxhighlight>


=={{header|C sharp|C#}}==
=={{header|C sharp|C#}}==
{
{
<lang csharp>using System;
<syntaxhighlight lang="csharp">using System;
using System.Linq;
using System.Linq;
using System.Text.RegularExpressions;
using System.Text.RegularExpressions;
Line 644: Line 644:
}
}
}
}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>US0378331005 is valid
<pre>US0378331005 is valid
Line 655: Line 655:


=={{header|C++}}==
=={{header|C++}}==
<lang cpp>
<syntaxhighlight lang="cpp">


#include <string>
#include <string>
Line 716: Line 716:
return 0;
return 0;
}
}
</syntaxhighlight>
</lang>


=={{header|Caché ObjectScript}}==
=={{header|Caché ObjectScript}}==


<lang cos>Class Utils.Check [ Abstract ]
<syntaxhighlight lang="cos">Class Utils.Check [ Abstract ]
{
{


Line 749: Line 749:
}
}


}</lang>
}</syntaxhighlight>
{{out|Examples}}
{{out|Examples}}
<pre>USER>For { Read isin Quit:isin="" Write ": "_##class(Utils.Check).ISIN(isin), ! }
<pre>USER>For { Read isin Quit:isin="" Write ": "_##class(Utils.Check).ISIN(isin), ! }
Line 763: Line 763:


=={{header|Clojure}}==
=={{header|Clojure}}==
<lang clojure>(defn luhn? [cc]
<syntaxhighlight lang="clojure">(defn luhn? [cc]
(let [sum (->> cc
(let [sum (->> cc
(map #(Character/digit ^char % 10))
(map #(Character/digit ^char % 10))
Line 783: Line 783:
"AU0000XVGZA3" "AU0000VXGZA3" "FR0000988040"]]
"AU0000XVGZA3" "AU0000VXGZA3" "FR0000988040"]]
(cl-format *out* "~A: ~:[invalid~;valid~]~%" isin (is-valid-isin? isin)))
(cl-format *out* "~A: ~:[invalid~;valid~]~%" isin (is-valid-isin? isin)))
</syntaxhighlight>
</lang>
<tt>luhn?</tt> is based on ''[[Luhn test of credit card numbers#Clojure]]''.
<tt>luhn?</tt> is based on ''[[Luhn test of credit card numbers#Clojure]]''.
{{out}}
{{out}}
Line 796: Line 796:
=={{header|COBOL}}==
=={{header|COBOL}}==
{{works with|GnuCOBOL}}
{{works with|GnuCOBOL}}
<lang cobol> >>SOURCE FORMAT FREE
<syntaxhighlight lang="cobol"> >>SOURCE FORMAT FREE
*> this is gnucobol 2.0
*> this is gnucobol 2.0
identification division.
identification division.
Line 968: Line 968:
goback
goback
.
.
end program luhntest.</lang>
end program luhntest.</syntaxhighlight>


{{out}}
{{out}}
Line 981: Line 981:


=={{header|Common Lisp}}==
=={{header|Common Lisp}}==
<lang lisp>(defun alphap (char)
<syntaxhighlight lang="lisp">(defun alphap (char)
(char<= #\A char #\Z))
(char<= #\A char #\Z))


Line 1,018: Line 1,018:
(dolist (isin '("US0378331005" "US0373831005" "U50378331005" "US03378331005"
(dolist (isin '("US0378331005" "US0373831005" "U50378331005" "US03378331005"
"AU0000XVGZA3" "AU0000VXGZA3" "FR0000988040"))
"AU0000XVGZA3" "AU0000VXGZA3" "FR0000988040"))
(format t "~A: ~:[invalid~;valid~]~%" isin (valid-isin-p isin))))</lang>
(format t "~A: ~:[invalid~;valid~]~%" isin (valid-isin-p isin))))</syntaxhighlight>
{{out}}
{{out}}
<pre>US0378331005: valid
<pre>US0378331005: valid
Line 1,031: Line 1,031:
{{trans|Java}}
{{trans|Java}}
Code for the luhn test was taken from [[https://rosettacode.org/wiki/Luhn_test_of_credit_card_numbers#D]]
Code for the luhn test was taken from [[https://rosettacode.org/wiki/Luhn_test_of_credit_card_numbers#D]]
<lang D>import std.stdio;
<syntaxhighlight lang="d">import std.stdio;


void main() {
void main() {
Line 1,070: Line 1,070:
import luhn;
import luhn;
return luhnTest(sb.data);
return luhnTest(sb.data);
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 1,083: Line 1,083:
=={{header|Elixir}}==
=={{header|Elixir}}==
used Luhn module from [[Luhn_test_of_credit_card_numbers#Elixir | here]]
used Luhn module from [[Luhn_test_of_credit_card_numbers#Elixir | here]]
<lang elixir>isin? = fn str ->
<syntaxhighlight lang="elixir">isin? = fn str ->
if str =~ ~r/\A[A-Z]{2}[A-Z0-9]{9}\d\z/ do
if str =~ ~r/\A[A-Z]{2}[A-Z0-9]{9}\d\z/ do
String.codepoints(str)
String.codepoints(str)
Line 1,101: Line 1,101:
AU0000VXGZA3
AU0000VXGZA3
FR0000988040)
FR0000988040)
|> Enum.each(&IO.puts "#{&1}\t#{isin?.(&1)}")</lang>
|> Enum.each(&IO.puts "#{&1}\t#{isin?.(&1)}")</syntaxhighlight>


{{out}}
{{out}}
Line 1,117: Line 1,117:
=={{header|Factor}}==
=={{header|Factor}}==
We re-use the <code>luhn?</code> word from ''[[Luhn test of credit card numbers#Factor]]''.
We re-use the <code>luhn?</code> word from ''[[Luhn test of credit card numbers#Factor]]''.
<lang factor>USING: combinators.short-circuit.smart formatting kernel luhn
<syntaxhighlight lang="factor">USING: combinators.short-circuit.smart formatting kernel luhn
math math.parser qw sequences strings unicode ;
math math.parser qw sequences strings unicode ;
IN: rosetta-code.isin
IN: rosetta-code.isin
Line 1,154: Line 1,154:
] each ;
] each ;
MAIN: main</lang>
MAIN: main</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,168: Line 1,168:
=={{header|Fortran}}==
=={{header|Fortran}}==


<lang fortran>program isin
<syntaxhighlight lang="fortran">program isin
use ctype
use ctype
implicit none
implicit none
Line 1,224: Line 1,224:
check_isin = 0 == mod(v, 10)
check_isin = 0 == mod(v, 10)
end function
end function
end program</lang>
end program</syntaxhighlight>


=={{header|FreeBASIC}}==
=={{header|FreeBASIC}}==
<lang freebasic>' version 27-10-2016
<syntaxhighlight lang="freebasic">' version 27-10-2016
' compile with: fbc -s console
' compile with: fbc -s console


Line 1,304: Line 1,304:
Print : Print "hit any key to end program"
Print : Print "hit any key to end program"
Sleep
Sleep
End</lang>
End</syntaxhighlight>
{{out}}
{{out}}
<pre>US0378331005 Valid
<pre>US0378331005 Valid
Line 1,316: Line 1,316:
=={{header|Go}}==
=={{header|Go}}==


<lang go>package main
<syntaxhighlight lang="go">package main


import "regexp"
import "regexp"
Line 1,345: Line 1,345:
sum += int(n[11] - '0')
sum += int(n[11] - '0')
return sum%10 == 0
return sum%10 == 0
}</lang>
}</syntaxhighlight>


<lang go>package main
<syntaxhighlight lang="go">package main


import "testing"
import "testing"
Line 1,372: Line 1,372:
}
}
}
}
}</lang>
}</syntaxhighlight>


=={{header|Groovy}}==
=={{header|Groovy}}==
Line 1,378: Line 1,378:
{{update|Groovy|Use the new test-cases, and consider calling the existing Luhn algorithm implementation from the ''[[Luhn test of credit card numbers]]'' task instead of duplicating it.}}
{{update|Groovy|Use the new test-cases, and consider calling the existing Luhn algorithm implementation from the ''[[Luhn test of credit card numbers]]'' task instead of duplicating it.}}


<lang groovy>CHARS = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'
<syntaxhighlight lang="groovy">CHARS = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'


int checksum(String prefix) {
int checksum(String prefix) {
Line 1,390: Line 1,390:
assert checksum('GB000263494') == 6
assert checksum('GB000263494') == 6
assert checksum('US037833100') == 5
assert checksum('US037833100') == 5
assert checksum('US037833107') == 0</lang>
assert checksum('US037833107') == 0</syntaxhighlight>


=={{header|Haskell}}==
=={{header|Haskell}}==
<lang Haskell>module ISINVerification2 where
<syntaxhighlight lang="haskell">module ISINVerification2 where


import Data.Char (isUpper, isDigit, digitToInt)
import Data.Char (isUpper, isDigit, digitToInt)
Line 1,474: Line 1,474:
, "FR0000988040"
, "FR0000988040"
]
]
mapM_ printSolution isinnumbers</lang>
mapM_ printSolution isinnumbers</syntaxhighlight>


{{out}}
{{out}}
Line 1,486: Line 1,486:


Or, making alternative choices from the standard libraries:
Or, making alternative choices from the standard libraries:
<lang haskell>import Control.Monad ((<=<))
<syntaxhighlight lang="haskell">import Control.Monad ((<=<))
import Data.Bifunctor (first)
import Data.Bifunctor (first)
import Data.List (foldl') -- '
import Data.List (foldl') -- '
Line 1,550: Line 1,550:
"AU0000VXGZA3",
"AU0000VXGZA3",
"FR0000988040"
"FR0000988040"
]</lang>
]</syntaxhighlight>
{{Out}}
{{Out}}
<pre>("US0378331005",True)
<pre>("US0378331005",True)
Line 1,563: Line 1,563:


'''Solution:'''
'''Solution:'''
<lang j>require'regex'
<syntaxhighlight lang="j">require'regex'
validFmt=: 0 -: '^[A-Z]{2}[A-Z0-9]{9}[0-9]{1}$'&rxindex
validFmt=: 0 -: '^[A-Z]{2}[A-Z0-9]{9}[0-9]{1}$'&rxindex


Line 1,569: Line 1,569:
luhn=: 0 = 10 (| +/@,) 10 #.inv 1 2 *&|: _2 "."0\ |. NB. as per task Luhn_test_of_credit_card_numbers#J
luhn=: 0 = 10 (| +/@,) 10 #.inv 1 2 *&|: _2 "."0\ |. NB. as per task Luhn_test_of_credit_card_numbers#J


validISIN=: validFmt *. luhn@df36</lang>
validISIN=: validFmt *. luhn@df36</syntaxhighlight>


'''Required Examples:'''
'''Required Examples:'''
<lang j> Tests=: 'US0378331005';'US0373831005';'U50378331005';'US03378331005';'AU0000XVGZA3';'AU0000VXGZA3';'FR0000988040'
<syntaxhighlight lang="j"> Tests=: 'US0378331005';'US0373831005';'U50378331005';'US03378331005';'AU0000XVGZA3';'AU0000VXGZA3';'FR0000988040'
validISIN&> Tests
validISIN&> Tests
1 0 0 0 1 1 1</lang>
1 0 0 0 1 1 1</syntaxhighlight>


=={{header|Java}}==
=={{header|Java}}==
As the Luhn test method from the ''[[Luhn test of credit card numbers]]'' task is only a few lines, it has been embedded in the ISIN class for convenience.
As the Luhn test method from the ''[[Luhn test of credit card numbers]]'' task is only a few lines, it has been embedded in the ISIN class for convenience.


<lang java>public class ISIN {
<syntaxhighlight lang="java">public class ISIN {
public static void main(String[] args) {
public static void main(String[] args) {
Line 1,625: Line 1,625:
return (s1 + s2) % 10 == 0;
return (s1 + s2) % 10 == 0;
}
}
}</lang>
}</syntaxhighlight>


<pre>US0378331005 is valid
<pre>US0378331005 is valid
Line 1,638: Line 1,638:
{{works with|jq}}
{{works with|jq}}
'''Works with gojq, the Go implementation of jq'''
'''Works with gojq, the Go implementation of jq'''
<lang jq># This filter may be applied to integers or integer-valued strings
<syntaxhighlight lang="jq"># This filter may be applied to integers or integer-valued strings
def luhntest:
def luhntest:
def digits: tostring | explode | map([.]|implode|tonumber);
def digits: tostring | explode | map([.]|implode|tonumber);
Line 1,661: Line 1,661:
type == "string"
type == "string"
and test("^(?<cc>[A-Z][A-Z])(?<sc>[0-9A-Z]{9})(?<cs>[0-9])$")
and test("^(?<cc>[A-Z][A-Z])(?<sc>[0-9A-Z]{9})(?<cs>[0-9])$")
and (decodeBase36 | luhntest);</lang>
and (decodeBase36 | luhntest);</syntaxhighlight>
'''The Task'''
'''The Task'''
<lang jq>def task:
<syntaxhighlight lang="jq">def task:
"US0378331005",
"US0378331005",
"US0373831005",
"US0373831005",
Line 1,673: Line 1,673:
| . + " => " + (if is_ISIN then "valid" else "invalid" end);
| . + " => " + (if is_ISIN then "valid" else "invalid" end);


task</lang>
task</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,687: Line 1,687:


=={{header|Julia}}==
=={{header|Julia}}==
<lang julia>using Printf
<syntaxhighlight lang="julia">using Printf


luhntest(x) = luhntest(parse(Int, x))
luhntest(x) = luhntest(parse(Int, x))
Line 1,699: Line 1,699:
"US03378331005", "AU0000XVGZA3", "AU0000VXGZA3", "FR0000988040"]
"US03378331005", "AU0000XVGZA3", "AU0000VXGZA3", "FR0000988040"]
@printf("%-15s %5s\n", inum, ifelse(checkISIN(inum), "pass", "fail"))
@printf("%-15s %5s\n", inum, ifelse(checkISIN(inum), "pass", "fail"))
end</lang>
end</syntaxhighlight>


{{out}}
{{out}}
Line 1,712: Line 1,712:
=={{header|Kotlin}}==
=={{header|Kotlin}}==
As the Luhn test method is only a few lines, it's reproduced here for convenience:
As the Luhn test method is only a few lines, it's reproduced here for convenience:
<lang scala>// version 1.1
<syntaxhighlight lang="scala">// version 1.1


object Isin {
object Isin {
Line 1,748: Line 1,748:
println("$isin\t -> ${if (Isin.isValid(isin)) "valid" else "not valid"}")
println("$isin\t -> ${if (Isin.isValid(isin)) "valid" else "not valid"}")
}
}
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 1,765: Line 1,765:


{{works with|langur|0.8.10}}
{{works with|langur|0.8.10}}
<lang langur>val .luhntest = f(.s) {
<syntaxhighlight lang="langur">val .luhntest = f(.s) {
val .t = [0, 2, 4, 6, 8, 1, 3, 5, 7, 9]
val .t = [0, 2, 4, 6, 8, 1, 3, 5, 7, 9]
val .numbers = s2n .s
val .numbers = s2n .s
Line 1,794: Line 1,794:
write .key, ": ", .pass
write .key, ": ", .pass
writeln if(.pass == .tests[.key]: ""; " (ISIN TEST FAILED)")
writeln if(.pass == .tests[.key]: ""; " (ISIN TEST FAILED)")
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 1,806: Line 1,806:


=={{header|Lua}}==
=={{header|Lua}}==
<lang Lua>function luhn (n)
<syntaxhighlight lang="lua">function luhn (n)
local revStr, s1, s2, digit, mod = n:reverse(), 0, 0
local revStr, s1, s2, digit, mod = n:reverse(), 0, 0
for pos = 1, #revStr do
for pos = 1, #revStr do
Line 1,842: Line 1,842:
"FR0000988040"
"FR0000988040"
}
}
for _, ISIN in pairs(testCases) do print(ISIN, checkISIN(ISIN)) end</lang>
for _, ISIN in pairs(testCases) do print(ISIN, checkISIN(ISIN)) end</syntaxhighlight>
{{out}}
{{out}}
<pre>US0378331005 true
<pre>US0378331005 true
Line 1,853: Line 1,853:


=={{header|Mathematica}} / {{header|Wolfram Language}}==
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<lang Mathematica>ClearAll[LuhnQ, VakudISINQ]
<syntaxhighlight lang="mathematica">ClearAll[LuhnQ, VakudISINQ]
LuhnQ[n_Integer] := Block[{digits = Reverse@IntegerDigits@n}, Mod[Total[{digits[[;; ;; 2]], IntegerDigits[2 #] & /@ digits[[2 ;; ;; 2]]}, -1], 10] == 0]
LuhnQ[n_Integer] := Block[{digits = Reverse@IntegerDigits@n}, Mod[Total[{digits[[;; ;; 2]], IntegerDigits[2 #] & /@ digits[[2 ;; ;; 2]]}, -1], 10] == 0]
VakudISINQ[sin_String] := Module[{s = ToUpperCase[sin]},
VakudISINQ[sin_String] := Module[{s = ToUpperCase[sin]},
Line 1,868: Line 1,868:
]
]
]
]
VakudISINQ /@ {"US0378331005", "US0373831005", "U50378331005", "US03378331005", "AU0000XVGZA3", "AU0000VXGZA3", "FR0000988040"}</lang>
VakudISINQ /@ {"US0378331005", "US0373831005", "U50378331005", "US03378331005", "AU0000XVGZA3", "AU0000VXGZA3", "FR0000988040"}</syntaxhighlight>
{{out}}
{{out}}
<pre>{True, False, False, False, True, True, True}</pre>
<pre>{True, False, False, False, True, True, True}</pre>


=={{header|Nim}}==
=={{header|Nim}}==
<lang Nim>import strformat
<syntaxhighlight lang="nim">import strformat


const
const
Line 1,918: Line 1,918:
echo &"{isin} is valid."
echo &"{isin} is valid."
except ISINError:
except ISINError:
echo &"{isin} is not valid: {getCurrentExceptionMsg()}."</lang>
echo &"{isin} is not valid: {getCurrentExceptionMsg()}."</syntaxhighlight>


{{out}}
{{out}}
Line 1,931: Line 1,931:
=={{header|Perl}}==
=={{header|Perl}}==
We reuse the <tt>luhn_test()</tt> function from ''[[Luhn test of credit card numbers#Perl]]''.
We reuse the <tt>luhn_test()</tt> function from ''[[Luhn test of credit card numbers#Perl]]''.
<lang perl>use strict;
<syntaxhighlight lang="perl">use strict;
use English;
use English;
use POSIX;
use POSIX;
Line 1,951: Line 1,951:
split(//s, $isin));
split(//s, $isin));
return luhn_test($base10);
return luhn_test($base10);
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>1..7
<pre>1..7
Line 1,964: Line 1,964:
=={{header|Phix}}==
=={{header|Phix}}==
Note this (slightly better) version of Luhn() has the reverse() inside it, whereas the original did not.
Note this (slightly better) version of Luhn() has the reverse() inside it, whereas the original did not.
<!--<lang Phix>(phixonline)-->
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">Luhn</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">st</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">Luhn</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">st</span><span style="color: #0000FF;">)</span>
Line 2,007: Line 2,007:
<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;">"%s : %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">tests</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">],</span><span style="color: #000000;">reasons</span><span style="color: #0000FF;">[</span><span style="color: #000000;">valid_ISIN</span><span style="color: #0000FF;">(</span><span style="color: #000000;">tests</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">])+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]})</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;">"%s : %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">tests</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">],</span><span style="color: #000000;">reasons</span><span style="color: #0000FF;">[</span><span style="color: #000000;">valid_ISIN</span><span style="color: #0000FF;">(</span><span style="color: #000000;">tests</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">])+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</lang>-->
<!--</syntaxhighlight>-->
{{out}}
{{out}}
<pre>
<pre>
Line 2,021: Line 2,021:
=={{header|PicoLisp}}==
=={{header|PicoLisp}}==
Using the <tt>luhn</tt> function defined at ''[[Luhn test of credit card numbers#PicoLisp]]'':
Using the <tt>luhn</tt> function defined at ''[[Luhn test of credit card numbers#PicoLisp]]'':
<lang PicoLisp>(de isin (Str)
<syntaxhighlight lang="picolisp">(de isin (Str)
(let Str (mapcar char (chop Str))
(let Str (mapcar char (chop Str))
(and
(and
Line 2,043: Line 2,043:
"AU0000XVGZA3"
"AU0000XVGZA3"
"AU0000VXGZA3"
"AU0000VXGZA3"
"FR0000988040" ) ) )</lang>
"FR0000988040" ) ) )</syntaxhighlight>
{{out}}
{{out}}
<pre>(0 NIL NIL NIL 0 0 0)</pre>
<pre>(0 NIL NIL NIL 0 0 0)</pre>


=={{header|PowerShell}}==
=={{header|PowerShell}}==
<syntaxhighlight lang="powershell">
<lang PowerShell>
function Test-ISIN
function Test-ISIN
{
{
Line 2,112: Line 2,112:
(10 - ($sum % 10)) % 10 -match $checkDigit
(10 - ($sum % 10)) % 10 -match $checkDigit
}
}
</syntaxhighlight>
</lang>
<syntaxhighlight lang="powershell">
<lang PowerShell>
"US0378331005","US0373831005","US0337833103","AU0000XVGZA3","AU0000VXGZA3","FR0000988040" | ForEach-Object {
"US0378331005","US0373831005","US0337833103","AU0000XVGZA3","AU0000VXGZA3","FR0000988040" | ForEach-Object {
[PSCustomObject]@{
[PSCustomObject]@{
Line 2,120: Line 2,120:
}
}
}
}
</syntaxhighlight>
</lang>
{{Out}}
{{Out}}
<pre>
<pre>
Line 2,134: Line 2,134:


=={{header|PureBasic}}==
=={{header|PureBasic}}==
<lang PureBasic>EnableExplicit
<syntaxhighlight lang="purebasic">EnableExplicit


Procedure.b Check_ISIN(*c.Character)
Procedure.b Check_ISIN(*c.Character)
Line 2,182: Line 2,182:
CloseFile(0)
CloseFile(0)
EndIf
EndIf
Input()</lang>
Input()</syntaxhighlight>
{{Out}}
{{Out}}
<pre>US0378331005 TRUE
<pre>US0378331005 TRUE
Line 2,194: Line 2,194:
=={{header|Python}}==
=={{header|Python}}==


<lang python>def check_isin(a):
<syntaxhighlight lang="python">def check_isin(a):
if len(a) != 12 or not all(c.isalpha() for c in a[:2]) or not all(c.isalnum() for c in a[2:]):
if len(a) != 12 or not all(c.isalpha() for c in a[:2]) or not all(c.isalnum() for c in a[2:]):
return False
return False
Line 2,226: Line 2,226:
"AU0000XVGZA3", "AU0000VXGZA3", "FR0000988040"]]
"AU0000XVGZA3", "AU0000VXGZA3", "FR0000988040"]]


# [True, False, False, False, True, True, True]</lang>
# [True, False, False, False, True, True, True]</syntaxhighlight>


=={{header|Quackery}}==
=={{header|Quackery}}==
Line 2,232: Line 2,232:
<code>luhn</code> is defined at [[Luhn test of credit card numbers#Quackery]].
<code>luhn</code> is defined at [[Luhn test of credit card numbers#Quackery]].


<lang Quackery> [ 2 split drop do
<syntaxhighlight lang="quackery"> [ 2 split drop do
char A char z 1+ within
char A char z 1+ within
swap
swap
Line 2,263: Line 2,263:
$ "AU0000VXGZA3" task
$ "AU0000VXGZA3" task
$ "FR0000988040" task
$ "FR0000988040" task
</syntaxhighlight>
</lang>


{{out}}
{{out}}
Line 2,277: Line 2,277:
=={{header|Racket}}==
=={{header|Racket}}==


<lang racket>
<syntaxhighlight lang="racket">
#lang racket
#lang racket


Line 2,311: Line 2,311:
(map isin-test? test-cases)
(map isin-test? test-cases)
;; -> '(#t #f #f #f #t #t #t)
;; -> '(#t #f #f #f #t #t #t)
</syntaxhighlight>
</lang>


{{out}}
{{out}}
Line 2,323: Line 2,323:
Using the <tt>luhn-test</tt> function from the ''[[Luhn test of credit card numbers#Raku|Luhn test of credit card numbers]]'' task.
Using the <tt>luhn-test</tt> function from the ''[[Luhn test of credit card numbers#Raku|Luhn test of credit card numbers]]'' task.


<lang perl6>my $ISIN = /
<syntaxhighlight lang="raku" line>my $ISIN = /
^ <[A..Z]>**2 <[A..Z0..9]>**9 <[0..9]> $
^ <[A..Z]>**2 <[A..Z0..9]>**9 <[0..9]> $
<?{ luhn-test $/.comb.map({ :36($_) }).join }>
<?{ luhn-test $/.comb.map({ :36($_) }).join }>
Line 2,345: Line 2,345:
AU0000VXGZA3
AU0000VXGZA3
FR0000988040
FR0000988040
>;</lang>
>;</syntaxhighlight>


{{out}}
{{out}}
Line 2,359: Line 2,359:


=={{header|REXX}}==
=={{header|REXX}}==
<lang rexx>/*REXX program validates the checksum digit for an International Securities ID number.*/
<syntaxhighlight lang="rexx">/*REXX program validates the checksum digit for an International Securities ID number.*/
parse arg z /*obtain optional ISINs from the C.L.*/
parse arg z /*obtain optional ISINs from the C.L.*/
if z='' then z= "US0378331005 US0373831005 U50378331005 US03378331005 AU0000XVGZA3" ,
if z='' then z= "US0378331005 US0373831005 U50378331005 US03378331005 AU0000XVGZA3" ,
Line 2,385: Line 2,385:
$= $ + substr(y, j, 1) + left(_, 1) + substr(_, 2 , 1, 0)
$= $ + substr(y, j, 1) + left(_, 1) + substr(_, 2 , 1, 0)
end /*j*/ /* [↑] sum the odd and even digits.*/
end /*j*/ /* [↑] sum the odd and even digits.*/
return right($, 1)==0 /*return "1" if number passed Luhn test*/</lang>
return right($, 1)==0 /*return "1" if number passed Luhn test*/</syntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
<pre>
Line 2,398: Line 2,398:


=={{header|Ring}}==
=={{header|Ring}}==
<lang ring>
<syntaxhighlight lang="ring">
# Project : Validate International Securities Identification Number
# Project : Validate International Securities Identification Number


Line 2,479: Line 2,479:
next
next
return sumarr
return sumarr
</syntaxhighlight>
</lang>
Output:
Output:
<pre>
<pre>
Line 2,493: Line 2,493:
=={{header|Ruby}}==
=={{header|Ruby}}==
Using a pre-existing luhn method:
Using a pre-existing luhn method:
<lang ruby>RE = /\A[A-Z]{2}[A-Z0-9]{9}[0-9]{1}\z/
<syntaxhighlight lang="ruby">RE = /\A[A-Z]{2}[A-Z0-9]{9}[0-9]{1}\z/


def valid_isin?(str)
def valid_isin?(str)
Line 2,508: Line 2,508:
FR0000988040).map{|tc| valid_isin?(tc) }
FR0000988040).map{|tc| valid_isin?(tc) }
# => [true, false, false, false, true, true, true]</lang>
# => [true, false, false, false, true, true, true]</syntaxhighlight>


=={{header|Rust}}==
=={{header|Rust}}==


<lang Rust>extern crate luhn_cc;
<syntaxhighlight lang="rust">extern crate luhn_cc;


use luhn_cc::compute_luhn;
use luhn_cc::compute_luhn;
Line 2,556: Line 2,556:
return compute_luhn(number);
return compute_luhn(number);
}
}
</syntaxhighlight>
</lang>


=={{header|SAS}}==
=={{header|SAS}}==
<lang sas>data test;
<syntaxhighlight lang="sas">data test;
length isin $20 ok $1;
length isin $20 ok $1;
input isin;
input isin;
Line 2,606: Line 2,606:
FR0000988040
FR0000988040
;
;
run;</lang>
run;</syntaxhighlight>


=={{header|Scala}}==
=={{header|Scala}}==
{{Out}}Best seen running in your browser either by [https://scalafiddle.io/sf/D9ax4Js/0 ScalaFiddle (ES aka JavaScript, non JVM)] or [https://scastie.scala-lang.org/yOymYqoPSEeA7K7rjgn65g Scastie (remote JVM)].
{{Out}}Best seen running in your browser either by [https://scalafiddle.io/sf/D9ax4Js/0 ScalaFiddle (ES aka JavaScript, non JVM)] or [https://scastie.scala-lang.org/yOymYqoPSEeA7K7rjgn65g Scastie (remote JVM)].
<lang Scala>object Isin extends App {
<syntaxhighlight lang="scala">object Isin extends App {
val isins = Seq("US0378331005", "US0373831005", "U50378331005",
val isins = Seq("US0378331005", "US0373831005", "U50378331005",
"US03378331005", "AU0000XVGZA3","AU0000VXGZA3", "FR0000988040")
"US03378331005", "AU0000XVGZA3","AU0000VXGZA3", "FR0000988040")
Line 2,649: Line 2,649:
isins.foreach(isin => println(f"$isin is ${if (ISINtest(isin)) "" else "not"}%s valid"))
isins.foreach(isin => println(f"$isin is ${if (ISINtest(isin)) "" else "not"}%s valid"))


}</lang>
}</syntaxhighlight>


=={{header|SQL PL}}==
=={{header|SQL PL}}==
{{works with|Db2 LUW}} version 9.7 or higher.
{{works with|Db2 LUW}} version 9.7 or higher.
With SQL PL:
With SQL PL:
<lang sql pl>
<syntaxhighlight lang="sql pl">
--#SET TERMINATOR @
--#SET TERMINATOR @


Line 2,707: Line 2,707:
RETURN RET;
RETURN RET;
END @
END @
</syntaxhighlight>
</lang>
Output:
Output:
<pre>
<pre>
Line 2,773: Line 2,773:


=={{header|Tcl}}==
=={{header|Tcl}}==
<lang Tcl>package require Tcl 8.6 ;# mostly needed for [assert]. Substitute a simpler one or a NOP if required.</lang>
<syntaxhighlight lang="tcl">package require Tcl 8.6 ;# mostly needed for [assert]. Substitute a simpler one or a NOP if required.</syntaxhighlight>
A proc like assert is always good to have around. This one tries to report values used in its expression using subst:
A proc like assert is always good to have around. This one tries to report values used in its expression using subst:
<lang Tcl>proc assert {expr} { ;# for "static" assertions that throw nice errors
<syntaxhighlight lang="tcl">proc assert {expr} { ;# for "static" assertions that throw nice errors
if {![uplevel 1 [list expr $expr]]} {
if {![uplevel 1 [list expr $expr]]} {
set msg "{$expr}"
set msg "{$expr}"
Line 2,781: Line 2,781:
tailcall throw {ASSERT ERROR} $msg
tailcall throw {ASSERT ERROR} $msg
}
}
}</lang>
}</syntaxhighlight>
isin itself is a simple package. We compute the alphabet when the package is loaded in _init, because that's more fun than typing out the table:
isin itself is a simple package. We compute the alphabet when the package is loaded in _init, because that's more fun than typing out the table:
<lang Tcl>namespace eval isin {
<syntaxhighlight lang="tcl">namespace eval isin {
proc _init {} { ;# sets up the map used on every call
proc _init {} { ;# sets up the map used on every call
variable map
variable map
Line 2,819: Line 2,819:
}
}


}</lang>
}</syntaxhighlight>
To run the test suite, we use the tcltest framework included with Tcl:
To run the test suite, we use the tcltest framework included with Tcl:
<lang Tcl>package require tcltest
<syntaxhighlight lang="tcl">package require tcltest


tcltest::test isin-1 "Test isin validation" -body {
tcltest::test isin-1 "Test isin validation" -body {
Line 2,840: Line 2,840:
}
}
return ok
return ok
} -result ok</lang>
} -result ok</syntaxhighlight>


=={{header|Transact-SQL}}==
=={{header|Transact-SQL}}==


<syntaxhighlight lang="transact-sql">
<lang Transact-SQL>
CREATE FUNCTION dbo._ISINCheck( @strISIN VarChar(40) )
CREATE FUNCTION dbo._ISINCheck( @strISIN VarChar(40) )
RETURNS bit
RETURNS bit
Line 2,886: Line 2,886:
RETURN @bValid
RETURN @bValid
END
END
</syntaxhighlight>
</lang>
Testing
Testing
<syntaxhighlight lang="transact-sql">
<lang Transact-SQL>
-- Testing. The following tests all pass.
-- Testing. The following tests all pass.
;WITH ISIN_Tests AS
;WITH ISIN_Tests AS
Line 2,902: Line 2,902:
)
)
SELECT ISIN, Expected, dbo._ISINCheck(ISIN) AS TestResult FROM ISIN_Tests ORDER BY ISIN
SELECT ISIN, Expected, dbo._ISINCheck(ISIN) AS TestResult FROM ISIN_Tests ORDER BY ISIN
</syntaxhighlight>
</lang>


=={{header|VBScript}}==
=={{header|VBScript}}==
<lang vb>' Validate International Securities Identification Number - 03/03/2019
<syntaxhighlight lang="vb">' Validate International Securities Identification Number - 03/03/2019


buf=buf&test("US0378331005")&vbCrLf
buf=buf&test("US0378331005")&vbCrLf
Line 2,951: Line 2,951:
end if
end if
test=cc&" "&msg
test=cc&" "&msg
end function 'test </lang>
end function 'test </syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 2,966: Line 2,966:
{{works with|Visual Basic|VB6 Standard}}
{{works with|Visual Basic|VB6 Standard}}
Calls LuhnCheckPassed() function described at [[Luhn_test_of_credit_card_numbers#Visual_Basic]]
Calls LuhnCheckPassed() function described at [[Luhn_test_of_credit_card_numbers#Visual_Basic]]
<lang vb>Function IsValidISIN(ByVal ISIN As String) As Boolean
<syntaxhighlight lang="vb">Function IsValidISIN(ByVal ISIN As String) As Boolean
Dim s As String, c As String
Dim s As String, c As String
Dim i As Long
Dim i As Long
Line 2,985: Line 2,985:
IsValidISIN = LuhnCheckPassed(s)
IsValidISIN = LuhnCheckPassed(s)
End If
End If
End Function</lang>
End Function</syntaxhighlight>
Test:
Test:
<lang vb>Sub Main()
<syntaxhighlight lang="vb">Sub Main()
Debug.Assert IsValidISIN("US0378331005")
Debug.Assert IsValidISIN("US0378331005")
Debug.Assert Not IsValidISIN("US0373831005")
Debug.Assert Not IsValidISIN("US0373831005")
Line 2,996: Line 2,996:
Debug.Assert IsValidISIN("FR0000988040")
Debug.Assert IsValidISIN("FR0000988040")
Debug.Assert Not IsValidISIN("FR000098804O")
Debug.Assert Not IsValidISIN("FR000098804O")
End Sub</lang>
End Sub</syntaxhighlight>


=={{header|Visual Basic .NET}}==
=={{header|Visual Basic .NET}}==
{{trans|C#}}
{{trans|C#}}
<lang vbnet>Option Strict On
<syntaxhighlight lang="vbnet">Option Strict On
Imports System.Text.RegularExpressions
Imports System.Text.RegularExpressions


Line 3,048: Line 3,048:
End Sub
End Sub


End Module</lang>
End Module</syntaxhighlight>
{{out}}
{{out}}
<pre>US0378331005 is valid
<pre>US0378331005 is valid
Line 3,060: Line 3,060:
=={{header|Vlang}}==
=={{header|Vlang}}==
{{trans|go}}
{{trans|go}}
<lang vlang>import regex
<syntaxhighlight lang="vlang">import regex


const (
const (
Line 3,114: Line 3,114:
}
}
}
}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>expected true for US0378331005, got false
<pre>expected true for US0378331005, got false
Line 3,127: Line 3,127:
{{libheader|Wren-fmt}}
{{libheader|Wren-fmt}}
The Luhn test method is reproduced here for convenience.
The Luhn test method is reproduced here for convenience.
<lang ecmascript>import "/str" for Char
<syntaxhighlight lang="ecmascript">import "/str" for Char
import "/trait" for Stepped
import "/trait" for Stepped
import "/fmt" for Conv, Fmt
import "/fmt" for Conv, Fmt
Line 3,166: Line 3,166:
var ans = (isin.call(test)) ? "valid" : "not valid"
var ans = (isin.call(test)) ? "valid" : "not valid"
System.print("%(Fmt.s(-13, test)) -> %(ans)")
System.print("%(Fmt.s(-13, test)) -> %(ans)")
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 3,180: Line 3,180:


=={{header|XPL0}}==
=={{header|XPL0}}==
<lang XPL0>string 0; \use zero-terminated strings
<syntaxhighlight lang="xpl0">string 0; \use zero-terminated strings


func Luhn(Str); \Return 'true' if digits in Str pass Luhn test
func Luhn(Str); \Return 'true' if digits in Str pass Luhn test
Line 3,232: Line 3,232:
CrLf(0);
CrLf(0);
];
];
]</lang>
]</syntaxhighlight>


{{out}}
{{out}}
Line 3,247: Line 3,247:
=={{header|Yabasic}}==
=={{header|Yabasic}}==
{{trans|FreeBASIC}}
{{trans|FreeBASIC}}
<lang Yabasic>sub luhntest(cardnr$)
<syntaxhighlight lang="yabasic">sub luhntest(cardnr$)
local i, j, s1, s2, l
local i, j, s1, s2, l
Line 3,301: Line 3,301:
if luhntest(test_str$) then print " Valid" else print " Invalid, checksum error" end if
if luhntest(test_str$) then print " Valid" else print " Invalid, checksum error" end if
loop
loop
</syntaxhighlight>
</lang>


=={{header|zkl}}==
=={{header|zkl}}==
Uses the luhn test from [[Luhn_test_of_credit_card_numbers#zkl]] (copied here as it is short).
Uses the luhn test from [[Luhn_test_of_credit_card_numbers#zkl]] (copied here as it is short).
<lang zkl>fcn validateISIN(isin){
<syntaxhighlight lang="zkl">fcn validateISIN(isin){
RegExp(String("^","[A-Z]"*2,"[A-Z0-9]"*9,"[0-9]$")).matches(isin) and
RegExp(String("^","[A-Z]"*2,"[A-Z0-9]"*9,"[0-9]$")).matches(isin) and
luhnTest(isin.split("").apply("toInt",36).concat().toInt())
luhnTest(isin.split("").apply("toInt",36).concat().toInt())
Line 3,312: Line 3,312:
0 == (n.split().reverse().reduce(fcn(s,n,clk){
0 == (n.split().reverse().reduce(fcn(s,n,clk){
s + if(clk.inc()%2) n else 2*n%10 + n/5 },0,Ref(1)) %10)
s + if(clk.inc()%2) n else 2*n%10 + n/5 },0,Ref(1)) %10)
}</lang>
}</syntaxhighlight>
<lang zkl>println(" ISIN Valid?");
<syntaxhighlight lang="zkl">println(" ISIN Valid?");
foreach isin in (T("US0378331005","US0373831005","U50378331005",
foreach isin in (T("US0378331005","US0373831005","U50378331005",
"US03378331005","AU0000XVGZA3","AU0000VXGZA3","FR0000988040")){
"US03378331005","AU0000XVGZA3","AU0000VXGZA3","FR0000988040")){
println(isin," --> ",validateISIN(isin));
println(isin," --> ",validateISIN(isin));
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>