Check Credit Card Number

CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.


‘ Usage: Returns Boolean indicating whether or not credit card
‘ number given passes the Luhn Formula (as described in
‘ ISO/IEC 7812-1:1993). Only numbers and numerical strings
‘ accepted, remove any spaces and dashes in strings.
‘ Example:
‘ booAcceptCard = CheckCC("4000000000000002")

Function CheckCC(CCNo)
Dim I, w, x, y

y = 0
CCNo = CStr(CCNo)

‘Process digits from right to left, drop last digit if total
‘length is even

w = 2 * (len(CCNo) Mod 2)

for I = len(CCNo) – 1 to 1 step -1
x = mid(CCNo, I, 1)

If IsNumeric(x) then
Select Case (I Mod 2) + w
Case 0, 3

‘Even Digit – Odd where total length is odd
‘(eg. Visa vs. Amx)

y = y + CInt(x)
Case 1, 2

‘Odd Digit – Even where total length is odd
‘ (eg. Visa vs. Amx)

x = CInt(x) * 2
If x > 9 then

‘Break the digits (eg. 19 becomes 1 + 9)

y = y + (x 10) + (x – 10)
else
y = y + x
End If
End Select
End If
next

‘Return the 10’s complement of the total
y = 10 – (y Mod 10)

If y > 9 then y = 0
CheckCC = (CStr(y) = Right$(CCNo, 1))
End Function

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read