TIP: Validating Credit Card Numbers

Thursday Sep 19th 2002 by Sam Huggill
Share:

This Visual basic code works by accepting a credit card number and then returning the last number.

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

Private Sub cmdCheck_Click()
Msgbox CheckCard(txtNumber)
End Sub
Share:
Home
Mobile Site | Full Site
Copyright 2017 © QuinStreet Inc. All Rights Reserved