dcsimg
December 7, 2016
Hot Topics:

TIP: Validating Credit Card Numbers

  • September 19, 2002
  • By James Johnston
  • Send Email »
  • More Articles »

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.

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

Function IsEven(lngNumber As Long) As Boolean

If lngNumber Mod 2 = 0 Then
  IsEven = True
Else
  IsEven = False
End If

End Function





Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

Thanks for your registration, follow us on our social networks to keep up-to-date
Rocket Fuel