[COOKBOOK] CreditCard validation
FlexibleLearning.com
admin at FlexibleLearning.com
Mon Oct 21 03:50:00 EDT 2013
ccValidate
-- Given a credit card number, get the card type and if it is a valid
number.
-- It does not check if the card is stolen, has expired or is otherwise
suspect.
on closeField
ccValidate me,CardType,Validity
answer CardType &cr& Validity
end closeField
on ccValidate pNum, at CardType, at Validity
replace space with "" in pNum
replace "-" with "" in pNum
if (pNum is NOT a number) then
put "Not a card number" into CardType
put "Unable to validate" into Validity
else
--| Card identification by regex...
local isVisa ="^(?:4[0-9]{12}(?:[0-9]{3})?)$"
local isMasterCard= "^(?:5[1-5][0-9]{14})$"
local isDiscover ="^(?:6(?:011|5[0-9][0-9])[0-9]{12})$"
local isAMEX ="^(?:3[47][0-9]{13})$"
local isDinersClub ="^(?:3(?:0[0-5]|[68][0-9])[0-9]{11})$"
local isJCB ="^(?:(?:2131|1800|35\d{3})\d{11})$"
if matchText(pNum,isVisa) then put "VISA" into CardType
else if matchText(pNum,isMasterCard) then put "MASTERCARD" into CardType
else if matchText(pNum,isDiscover) then put "DISCOVER" into CardType
else if matchText(pNum,isAMEX) then put "AMERICANEXPRESS" into CardType
else if matchText(pNum,isDinersClub) then put "DINERSCLUB" into CardType
else if matchText(pNum,isJCB) then put "JCB" into CardType
else put "UNKNOWN CARD TYPE" into CardType
--| Checksum verification by Luhn algorithm...
put len(pNum) into tLen
repeat with z = 1 to tLen
put char -z of pNum into n
if z mod 2 =0 then
put n*2 into n
if n <10 then add n to stdout
else add (n mod 10) +1 to stdout
else
add n to stdout
end if
end repeat
if (stdout mod 10 =0) then put "valid card number" into Validity
else put "invalid card number" into Validity
end if
end ccValidate
by Hugh Senior
FLCo
More information about the use-livecode
mailing list