Validate International Securities Identification Number: Difference between revisions
Content added Content deleted
(Added BASIC256) |
|||
Line 543: | Line 543: | ||
1 FR0000988040 |
1 FR0000988040 |
||
</pre> |
</pre> |
||
=={{header|BASIC256}}== |
|||
<syntaxhighlight lang="vbnet">array base 1 |
|||
dim test_set$(7) |
|||
test_set$ = {"US0378331005", "US0373831005", "U50378331005", "US03378331005", "AU0000XVGZA3", "AU0000VXGZA3", "FR0000988040"} |
|||
for i = 1 to test_set$[?] |
|||
test_str$ = "" |
|||
l = length(test_set$[i]) |
|||
if l <> 12 then |
|||
print test_set$[i]; " Invalid, length <> 12 char." |
|||
continue for |
|||
end if |
|||
if asc(mid(test_set$[i], 1, 1)) < asc("A") or asc(mid(test_set$[i], 2, 1)) < asc("A") then |
|||
print test_set$[i]; " Invalid, number needs to start with 2 characters" |
|||
continue for |
|||
end if |
|||
for n = 1 to l |
|||
x = asc(mid(test_set$[i], n, 1)) - asc("0") |
|||
if x > 9 then x -= 7 |
|||
if x < 10 then |
|||
test_str$ += string(x) |
|||
else # two digest number |
|||
test_str$ += string(x \ 10) + string(x mod 10) |
|||
end if |
|||
next |
|||
print test_set$[i]; " "; |
|||
if luhntest(test_str$) = 1 then |
|||
print "Invalid, checksum error" |
|||
else |
|||
print "Valid" |
|||
end if |
|||
next |
|||
end |
|||
function luhntest(cardnr$) |
|||
cardnr$ = trim(cardnr$) # remove spaces |
|||
l = length(cardnr$) |
|||
s1 = 0 |
|||
s2 = 0 |
|||
# sum odd numbers |
|||
for i = 1 to l step 2 |
|||
s1 += fromradix(asc(mid(cardnr$, i, 1)), 10) |
|||
next |
|||
# sum even numbers |
|||
for i = 2 to l step 2 |
|||
j = fromradix(asc(mid(cardnr$, i, 1)), 10) |
|||
j *= 2 |
|||
if j > 9 then j = (j mod 10) + 1 |
|||
s2 += j |
|||
next |
|||
return (s1 + s2) mod 10 = 0 |
|||
end function</syntaxhighlight> |
|||
{{out}} |
|||
<pre>Similar to FreeBASIC entry.</pre> |
|||
=={{header|C}}== |
=={{header|C}}== |