PDA

View Full Version : Credit Card Luhn Check



dude9er
01-25-2007, 11:02 PM
I'm trying to merge my credit card validation with the below Luhn Algorithm validation



function CheckCC(CC_NUM)
Dim i, w, x, y

InputError=0
CC_NUM = Replace(Replace(Replace(CStr(CC_NUM), "-", ""), " ", ""), ".", "")
y = 0
'Process digits from right to left, drop last digit if total length is even
w = 2 * (Len(CC_NUM) Mod 2)
For i = Len(CC_NUM) - 1 To 1 Step -1
x = Mid(CC_NUM, 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(CC_NUM, 1))

End function


The code I have been using works great and allows for InputErrors in hidden fields. I want to try and merge the two and allow the above Luhn validation to target textField "CC_NUM" and place a response error into hidden field "Reason" if the crdit card is bad.

When the above function is called, how do I incorporate an error to tell the user that the credit card is invalid?

Here is my code:



dim InputError,Reason,Reason2,Reason3

InputError=0
'check for numeric vals in str
'if no pass test, no need to continue proc
If Not IsNumeric(CC_NUM) Then
Reason = "<strong>ERROR:</strong> Card Number contains invalid characters. Use Numbers ONLY without hyphens or spaces."
InputError=InputError+1
ELSE
if CC_TYPE = "AMEX" AND Len(CC_NUM) <> 15 THEN
Reason = "<strong>ERROR:</strong> AMEX Number is " & Len(CC_NUM) & " Digits Long instead of 15."
InputError=InputError+1
else
if CC_TYPE = "VISA" AND Len(CC_NUM) = 13 Or Len(CC_NUM) = 16 THEN
Else
InputError=InputError+1
Reason = "<strong>ERROR:</strong> Visa Card Number is " & Len(CC_NUM) & " Digits Long instead of 13 or 16."
InputError=InputError+1
end if
if CC_TYPE = "MasterCard" AND Len(CC_NUM) <> 16 THEN
Reason = "<strong>ERROR:</strong> MasterCard Number is " & Len(CC_NUM) & " Digits Long instead of 16."
InputError=InputError+1
end if
'===============================================
if CC_TYPE = "AMEX" AND Len(SecCode) <> 4 THEN
Reason3 = "<strong>ERROR:</strong> AMEX Security Code should contain <strong>4 </strong>Digits."
InputError=InputError+1
end if

if CC_TYPE = "VISA" AND Len(SecCode) <> 3 THEN
Reason3 = "<strong>ERROR:</strong> Visa Security Code should contain <strong>3 </strong>Digits."
InputError=InputError+1
end if

if CC_TYPE = "MasterCard" AND Len(SecCode) <> 3 THEN
Reason3 = "<strong>ERROR:</strong> MasterCard Security Code should contain <strong>3</strong> Digits."
InputError=InputError+1
end if

End if
end if


Thanks everyone in advance!!