by Waty Thierry (60 Submissions)
Category: Math/Dates
Compatability: Visual Basic 4.0 (32-bit)
Difficulty: Unknown Difficulty
Originally Published: Tue 13th April 1999
Date Added: Mon 8th February 2021
Rating: (2 Votes)
Validating Credit Card Numbers
Const MAX_DIGITS = 20 ' actually don't know any card using more than 16 digits
Dim anDigits(1 To MAX_DIGITS) As Byte
Dim nDigits As Long
Dim ofsCurrentDigit As Long
Dim ofsCurrentCharacter As Long
Dim CurrentCharacter As String
Dim Multiplier As Long
Dim CheckSum As Long
Dim DigitValue As Long
Dim ValidDigits As String
If Len(Trim$(sCardNo)) < 1 Then
Result = False
GoTo Exit_Point
End If
ValidDigits = "0123456789"
For ofsCurrentCharacter = 1 To Len(sCardNo)
CurrentCharacter = Mid$(sCardNo, ofsCurrentCharacter, 1)
If InStr(1, ValidDigits, CurrentCharacter, vbBinaryCompare) Then
nDigits = nDigits + 1
If nDigits > MAX_DIGITS Then
Result = False
GoTo Exit_Point
End If
anDigits(nDigits) = Val(CurrentCharacter)
End If
Next ofsCurrentCharacter
CheckSum = anDigits(nDigits)
For ofsCurrentDigit = nDigits - 1 To 1 Step -1
If Multiplier = 2 Then
Multiplier = 1
Else
Multiplier = 2
End If
DigitValue = anDigits(ofsCurrentDigit) * Multiplier
CheckSum = CheckSum + DigitValue
If DigitValue > 9 Then
CheckSum = CheckSum - 9
End If
Next ofsCurrentDigit
Result = ((CheckSum Mod 10) = 0)
Exit_Point:
IsValidCreditCardNumber = Result
Exit Function
End Function