How to resolve the algorithm Validate International Securities Identification Number step by step in the Visual Basic .NET programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Validate International Securities Identification Number step by step in the Visual Basic .NET programming language

Table of Contents

Problem Statement

An International Securities Identification Number (ISIN) is a unique international identifier for a financial security such as a stock or bond.

Write a function or program that takes a string as input, and checks whether it is a valid ISIN. It is only valid if it has the correct format,   and   the embedded checksum is correct. Demonstrate that your code passes the test-cases listed below.

The format of an ISIN is as follows:

For this task, you may assume that any 2-character alphabetic sequence is a valid country code. The checksum can be validated as follows:

(The comments are just informational.   Your function should simply return a Boolean result.   See #Raku for a reference solution.)

Related task:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Validate International Securities Identification Number step by step in the Visual Basic .NET programming language

Source code in the visual programming language

Option Strict On
Imports System.Text.RegularExpressions

Module Module1
    ReadOnly IsinRegex As New Regex("^[A-Z]{2}[A-Z0-9]{9}\d$", RegexOptions.Compiled)

    Function DigitValue(c As Char) As Integer
        Dim temp As Integer
        If Asc(c) >= Asc("0"c) AndAlso Asc(c) <= Asc("9"c) Then
            temp = Asc(c) - Asc("0"c)
        Else
            temp = Asc(c) - Asc("A"c) + 10
        End If
        Return temp
    End Function

    Function LuhnTest(number As String) As Boolean
        Return number.Select(Function(c, i) (AscW(c) - 48) << ((number.Length - i - 1) And 1)).Sum(Function(n) If(n > 9, n - 9, n)) Mod 10 = 0
    End Function

    Function Digitize(isin As String) As String
        Return String.Join("", isin.Select(Function(c) $"{DigitValue(c)}"))
    End Function

    Function IsValidIsin(isin As String) As Boolean
        Return IsinRegex.IsMatch(isin) AndAlso LuhnTest(Digitize(isin))
    End Function

    Sub Main()
        Dim isins() = {
            "US0378331005",
            "US0373831005",
            "U50378331005",
            "US03378331005",
            "AU0000XVGZA3",
            "AU0000VXGZA3",
            "FR0000988040"
        }

        For Each isin In isins
            If IsValidIsin(isin) Then
                Console.WriteLine("{0} is valid", isin)
            Else
                Console.WriteLine("{0} is not valid", isin)
            End If
        Next
    End Sub

End Module


  

You may also check:How to resolve the algorithm Palindrome dates step by step in the C++ programming language
You may also check:How to resolve the algorithm Comma quibbling step by step in the EasyLang programming language
You may also check:How to resolve the algorithm Index finite lists of positive integers step by step in the D programming language
You may also check:How to resolve the algorithm Luhn test of credit card numbers step by step in the SparForte programming language
You may also check:How to resolve the algorithm Draw a sphere step by step in the Batch File programming language