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

# 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
• 2
1 Solution

Commented:
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

Commented:
Glad I could help! Thanks for the points!

Cheers!
0

## Featured Post

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