destruk
11 years agoStreaming Star
Luhn Credit Card Validation (code included)
I've been working on this a bit lately and thought I'd share a simplistic routine to verify credit card numbers.
This takes input as a numeric string and tells you if the card should be valid or not - for American Express, Discover, Visa, mastercard, Diner's Club, Maestro, and anything else out there (any number of digits)
Of course there can still be counterfeit cards, or invalid cvc codes or billing problems, but the payment processor takes care of that. This is simply another short check you can do before sending the number to your server.
It's short enough for me but I'm sure someone here could make it much more efficient. 🙂 Have fun.
This takes input as a numeric string and tells you if the card should be valid or not - for American Express, Discover, Visa, mastercard, Diner's Club, Maestro, and anything else out there (any number of digits)
Of course there can still be counterfeit cards, or invalid cvc codes or billing problems, but the payment processor takes care of that. This is simply another short check you can do before sending the number to your server.
Function Luhn(ccnumber As String) As String
N=[]
Tot=0
For x=LEN(ccnumber) To 0 Step -1
N.Push(ccnumber.mid(X,1).ToInt())
Next
For x=1 to LEN(ccnumber) Step 2
Tot=Tot+N[X]
Next
For x=2 to LEN(ccnumber) Step 2
N[X]=N[X]*2
If N[X]>9
Tot=Tot+1+(N[X]-10)
Else
Tot=Tot+N[X]
End If
Next
Print "Total: ";Tot
If (Tot.ToStr()).Right(1)="0"
Return "VALID CARD"
Else
Return "INVALID CARD"
End If
End Function
It's short enough for me but I'm sure someone here could make it much more efficient. 🙂 Have fun.