ValidateCreditCard




Function CheckCard(CCNumber As String) As Boolean
Dim Counter As Integer, TmpInt As Integer
Dim Answer As Integer
Counter = 1
TmpInt = 0
While Counter <= Len(CCNumber)
If IsEven(Len(CCNumber)) Then
TmpInt = Val(Mid$(CCNumber, Counter, 1))
If Not IsEven(Counter) Then
TmpInt = TmpInt * 2
If TmpInt > 9 Then TmpInt = TmpInt - 9
End If
Answer = Answer + TmpInt
'Debug.Print Counter, TmpInt, Answer

Counter = Counter + 1
Else
TmpInt = Val(Mid$(CCNumber, Counter, 1))
If IsEven(Counter) Then
TmpInt = TmpInt * 2
If TmpInt > 9 Then TmpInt = TmpInt - 9
End If
Answer = Answer + TmpInt
'Debug.Print Counter, TmpInt, Answer

Counter = Counter + 1
End If
Wend
Answer = Answer Mod 10
If Answer = 0 Then CheckCard = True
End Function

If the number is in a valid format if returns 'True'.
If the number is in an invalid format it returns 'False'.
This algorithm should work with all credit cards.
If a card validates with this code it doesn't mean that
the card is actually good, it just means that the numbers
are arranged in a valid format. If they don't validate
then you've saved some time because you don't have to
process the card to find out that it is definitely bad.
I use this function in CGI forms that process credit
card orders.










( validatecreditcard.html )- by Paolo Puglisi - Modifica del 17/12/2023