SHARE
Facebook X Pinterest WhatsApp

Check Credit Card Number

‘ ‘ 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 = […]

Written By
thumbnail
CodeGuru Staff
CodeGuru Staff
Jan 30, 2004
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

Recommended for you...

Using Multiple Programming Languages to Create an ASP.NET Website
Tariq Siddiqui
Jun 11, 2022
Visual Basic Features in Visual Studio 2022
Hannes DuPreez
Jan 25, 2022
Playing with Strings: Proper Case
Hannes DuPreez
May 27, 2020
Making a Small Lotto Game in VB.NET
Hannes DuPreez
Apr 28, 2020
CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2025 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.