Alain and I were thinking on the same wavelength the other day:
Independent of each other, he wrote a function using the Luhn MOD 10
to check the validity of a credit card number, and I wrote a function
which used the account number to determine the type of credit card to
which it was assigned.
Alain has very nicely merged our two functions into one which uses
the Luhn algorithm to check the validity of account number and return
the card type from the account number.
Of course, this code cannot determine if a given account is currently
active, in arrears, closed, etc. But it does perform a quick check to
determine whether or not the card account number is bogus.
The algorithm should work for all major bank cards: VISA, MasterCard,
American Express, JCB, Discover, enRoute and Diners Club/Carte
Blanche. We have checked it with some, but not all.
If you like, you may send me all your credit card numbers and I will
be glad to verify them. =:-O
What's that, you say...?!?!?
Okay, go ahead and check them yourselves, then.
Ken
Watch for e-mail line breaks and lost underscores before constants.
'----------- BEGIN FB^3 CODE ------------
'~FN CCVERIFY$ (Credit card verifier)
// Contributed 10-30-01
// Alain Pastor and Ken Shmidheiser
LOCAL MODE
LOCAL FN CCVerify$( ccNum AS STR63 )
DIM i AS LONG
DIM L AS LONG
DIM value AS LONG
DIM fourChars AS LONG
DIM twoChars AS INT
DIM cc( 15 ) AS INT
DIM ccType AS STR63
DIM temp AS STR63
DIM oneChar AS CHAR
ccType[0] = 0
value = 0
L = ccNum[0]
FOR i = 1 TO L
LONG IF ccNum[i] > = _"0" AND ccNum[i] < = _"9"
ccType + = CHR$( ccNum[i] )
XELSE
IF ccNum[i] != _" " THEN EXIT FOR
END IF
NEXT i
ccNum = ccType
L = ccNum[0]
LONG IF i > L
ccType = "Unknown Credit Card Type"
oneChar = ccNum[1]
twoChars = {@ccNum[1]}
fourchars = [@ccNum[1]]
SELECT L
CASE 16
SELECT
CASE oneChar = _"5" :
IF ccNum[2] > = _"1" AND ccNum[2] < = _"5"¬
THEN ccType = "MasterCard"
CASE oneChar = _"4" : ccType = "VISA"
CASE oneChar = _"3" : ccType = "JCB"
CASE fourchars = _"6011" : ccType = "Discover"
END SELECT
CASE 15
SELECT
CASE twoChars = _"34" OR twoChars = _"37"
ccType = "AMEX"
CASE fourchars = _"2014" OR fourchars = _"2149"
ccType = "enRoute"
CASE fourchars = _"2131" OR fourchars = _"1800"
ccType = "JCB"
END SELECT
CASE 14
SELECT
CASE twoChars = _"30"
LONG IF ccNum[3] > = _"1" AND ccNum[3] < = _"5"
ccType = "Diners Club/Carte Blanche"
END IF
CASE twoChars = _"36" OR twoChars = _"38"
ccType = "Diners Club/Carte Blanche"
END SELECT
CASE 13
IF oneChar = _"4" THEN ccType = "VISA"
END SELECT
LONG IF ccType != "Unknown Credit Card Type"
FOR i = 0 TO L - 1
cc( i) = val&( CHR$( ccNum[i + 1] ) )
LONG IF( i MOD 2 != 0 AND L MOD 2 != 0) ¬
OR( i MOD 2 = 0 AND L MOD 2 = 0 )
cc( i ) = cc( i ) < < 1
LONG IF cc( i ) > = 10
temp = STR$( cc( i ) )
cc( i ) = val&( CHR$( temp[2] ) ) ¬
+ val&( CHR$( temp[3] ) )
END IF
END IF
NEXT i
FOR i = 0 TO L - 1
value + = cc( i )
NEXT i
temp = STR$( value )
LONG IF temp[3] = _"0"
ccType = "Valid " + ccType + " Credit Card Number"
XELSE
ccType = "Invalid Credit Card Number"
END IF
END IF
END IF
END FN = ccType
DIM cc AS STR255
cc = "1111 1111 1111 1111"
PRINT FN CCVerify$( cc )
DO
UNTIL FN BUTTON
'-------------- END FB^3 CODE -------------