VB Code: Validate Credit Card Number

This is just a small VB code to validate credit cards number, all credit cards work with this algorthm;

If the number is in a valid format if returns 'True'.

If the number is in an invalid format it returns 'False'.

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. You can use this function in CGI forms that process credit card orders.

'Code
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

'end code

Previous Post
Next Post
Related Posts