• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 244
  • Last Modified:

Credit card algorithm in Visual Basic?

hi,

can anybody please pass me the sample algorithm foe validating credit cards in Visual Basic?

Thanks!!!

reagrds

David
0
thiamwah
Asked:
thiamwah
  • 2
1 Solution
 
mcriderCommented:
Here you go:


Function ValidateCreditCard(CardNumber As String) As Boolean
temp$ = ""
For i = 1 To Len(CardNumber)
    x$ = Mid$(CardNumber, i, 1)
    If x$ > Chr$(47) And x$ < Chr$(58) Then
        temp$ = temp$ + x$
    End If
Next i
j = Len(temp$) Mod 2
checksum = 0
If j = 0 Then
    'even number of numbers on credit card
    For i = 1 To Len(temp$) Step 2
        k = Val(Mid$(temp$, i, 1)) * 2
        If k > 9 Then
            k = k - 9
        End If
        checksum = checksum + k
    Next i
    For i = 2 To Len(temp$) Step 2
        k = Val(Mid$(temp$, i, 1))
        checksum = checksum + k
    Next i
Else
    'odd number of numbers on credit card
    For i = 2 To Len(temp$) Step 2
        k = Val(Mid$(temp$, i, 1)) * 2
        If k > 9 Then
            k = k - 9
        End If
        checksum = checksum + k
    Next i
    For i = 1 To Len(temp$) Step 2
        k = Val(Mid$(temp$, i, 1))
        checksum = checksum + k
    Next i
End If
checksum = checksum Mod 10
If checksum = 0 Then
    ValidateCreditCard = True
Else
    ValidateCreditCard = False
End If
End Function



Cheers!
0
 
mcriderCommented:
Glad I could help! Thanks for the points!


Cheers!
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now