Friday, 9 October 2015

Validating ISBN

If you have any databases that store information about books, you'll want some way of verifying ISBN entries. I'm not going to elaborate more about how ISBNs work, suffice to say they use modulus 11 maths. If you're really desperate to know, you can read all about it here. Anyway, this function takes one parameter, the ISBN-10 value, and returns True if it's a valid ISBN, False otherwise.

Function CheckISBN(x As Variant) As Boolean
    Dim bLen As Byte, iCnt As Integer, iVal As Integer
    bLen = Len(x)
    If bLen > 10 Or bLen = 0 Then GoTo Invalid_CheckISBN 'Number is too long or too short
    iVal = 0
    For iCnt = 10 To 1 Step -1
        If iCnt <= bLen Then
            iVal = iVal + (iCnt * Mid(x, ((-1 * iCnt) + bLen + 1), 1))
        End If
    Next iCnt
    CheckISBN = (iVal Mod 11 = 0)

    Exit Function

    CheckISBN = False
    GoTo Exit_CheckISBN
End Function

No comments:

Post a comment