Search code examples
vbscriptmoduloiban

VbScript validate IBAN with mod97 (number is too big)


So I am writing an application in VbScript and I am trying to validate an IBAN number. The problem though, the number I have to mod with, is too big.

For example: 734027177486111478 mod 97

that is what I want to do, but I can't seem to find a type to use for doing this. Underneath is the code I am using at the moment. In the case of the example, vCheckCode at the last point is 734027177486111478.

Function checkIBAN(ByVal vIban)
vLandcode = Mid(vIban, 1, 2)    

Select Case vLandcode
    Case "BE"
        vIban = Replace(vIban, " ", "")

        If Len(vIban) = 16 Then
            vPrefix = Mid(vIban, 1, 4)
            vCheckCode = Replace(vIban, vPrefix, "")
            vSuffix = Replace(vPrefix, "BE", "1114")
            vCheckCode = vCheckCode + vSuffix
            vCheckCode = CDbl(vCheckCode)
            vResult = vCheckCode Mod 97             
        End If
End Select
End Function

Any idea on how to do this?

Thanks in advance!


Solution

  • 734027177486111478 is too big to be represented as a 32-bit integer, so you can't just use Mod 97 like this.

    A simple solution is to use some basic math :

    • (a + b) mod n = ((a mod n) + (b mod n)) mod n
    • (a * b) mod n = ((a mod n) * (b mod n)) mod n

    And from that you can compute your modulo digit by digit:

    Function Mod97(ByVal vIban)
        Dim i, m, digit
        m = 0
        For i = 1 To Len(vIban)
            digit = CInt(Mid(vIban, i, 1))
            m = (10*m + digit) Mod 97
        Next
        Mod97 = m
    End Function
    

    Mod97("734027177486111478") returns 1, which looks correct.