Validate International Securities Identification Number: Difference between revisions

Content added Content deleted
m (→‎{{header|Perl 6}}: 'bare' regex no longer allowed with ternary)
Line 2,314: Line 2,314:


=={{header|Visual Basic}}==
=={{header|Visual Basic}}==
{{works with|Visual Basic|VB6 Standard}}

Calls LuhnCheckPassed() function described at [[Luhn_test_of_credit_card_numbers#Visual_Basic]]
{{update|Visual Basic|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 vb>Function IsValidISIN(ByVal ISIN As String) As Boolean

Dim s As String, c As String
{{works with|VB6}}
Dim i As Long
<lang vb>
If Len(ISIN) = 12 Then
Option Explicit
For i = 1 To Len(ISIN)

c = UCase$(Mid(ISIN, i, 1))
Function MakeIsinCode(Exchange As String, security As String)
Select Case c
Dim numLeadingZeroes As Integer
Case "A" To "Z"
If i = 12 Then Exit Function
numLeadingZeroes = 9 - Len(security)
s = s & CStr(Asc(c) - 55)
Dim leader As String
Case "0" To "9"
If i < 3 Then Exit Function
s = s & c
leader = Exchange & String(numLeadingZeroes, "0") & security
Case Else
Exit Function
MakeIsinCode = leader & CStr(IsinCheckDigit(leader))
End Select
End Function
Next i

IsValidISIN = LuhnCheckPassed(s)
Function IsinCheckDigit(ByVal security As String) As Integer
End If
Dim digits As String
End Function</lang>
Test:
Dim i As Integer
<lang vb>Sub Main()
Debug.Assert IsValidISIN("US0378331005")
For i = 1 To Len(security)
Debug.Assert Not IsValidISIN("US0373831005")
Dim ch As String
Debug.Assert Not IsValidISIN("U50378331005")
Debug.Assert Not IsValidISIN("US03378331005")
ch = UCase(Mid(security, i, 1))
Debug.Assert IsValidISIN("AU0000XVGZA3")
Debug.Assert IsValidISIN("AU0000VXGZA3")
If ch >= "A" And ch <= "Z" Then
Debug.Assert IsValidISIN("FR0000988040")
' A to Z translated to "10", "11", .. "35"
Debug.Assert Not IsValidISIN("FR000098804O")
digits = digits & CStr(Asc(ch) - 55)
End Sub</lang>
ElseIf ch >= "0" And ch <= "9" Then
digits = digits & ch
Else
Err.Raise 50001, , "Security must contain only letters and digits"
End If
Next
Dim total As Integer
Dim tmp As Integer
total = 0
'If rightmost even, "other" digits for doubling are 2,4,6. If rightmost odd, they're 1,3,5.
'rightmost digit is always doubled, so start with it and work backwards
Dim other As Boolean
other = True
For i = Len(digits) To 1 Step -1
tmp = CInt(Mid(digits, i, 1))
If other Then
If tmp < 5 Then
' 0 to 4 map to 0,2,4,6,8
total = total + (tmp * 2)
Else
' 5 to 9 map to 1,3,5,7,9
total = total + ((tmp * 2) - 9)
End If
Else
total = total + tmp
End If
'Toggle doubling flag
other = Not other
Next
'Last Mod 10 is to wrap 10 to zero
IsinCheckDigit = (10 - (total Mod 10)) Mod 10
End Function
</lang>


=={{header|Yabasic}}==
=={{header|Yabasic}}==