VB icon

Credit Card Validation (Luhn Formula)

Email
Submitted on: 1/5/2015 1:41:00 PM
By: BrewGuru99 (from psc cd)  
Level: Beginner
User Rating: By 4 Users
Compatibility: ASP (Active Server Pages), VbScript (browser/client side)
Views: 2115
 
     Uses the Luhn formula to quickly validate a credit card. Basically all the digits except for the last one are summed together and the output is a single digit (0 to 9). This digit is compared with the last digit ensure a proper credit card number is entered (Does not actually confirm that is is a real number, just that it is likely to be one. Example: Entering "4000-0000-0000-0002" will pass the check, but "4000-0000-0000-0003" will not pass.)
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
'**************************************
' Name: Credit Card Validation (Luhn Formula)
' Description:Uses the Luhn formula to quickly validate a credit card. Basically all the digits except for the last one are summed together and the output is a single digit (0 to 9). This digit is compared with the last digit ensure a proper credit card number is entered (Does not actually confirm that is is a real number, just that it is likely to be one. Example: Entering "4000-0000-0000-0002" will pass the check, but "4000-0000-0000-0003" will not pass.)
' By: BrewGuru99 (from psc cd)
'**************************************

Function CheckCC(CCNo)
 Dim i, w, x, y
 
 y = 0
 CCNo = Replace(Replace(Replace(CStr(CCNo), "-", ""), " ", ""), ".", "") 'Ensure proper format of the input
 '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


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.