credit card validation
[1/4] from: ralph::abooks::com at: 16-Sep-2000 13:59
For those interested in using REBOL for e-commerce (as we are here heavily
now), credit card validation using the LUHN (Mod 10) method (for what that
is and why you'd want to use it, see
http://www.beachnet.com/~hstiles/cardtype.html) seems moderately easy.
Below is how I'm doing it online now. For a fuller explanation, you'll have
to wait for the REBOL e-commerce book I'm working on.
Any suggestions on improving this function will be gratefully received.
Enjoy.
--Ralph Roberts
;;;;;;;;;; start credit card validation function ;;;;;;;;;;;;;
validate: func [card][
cleancard: func [card][
replace/all card " " ""
parse card [some [to "^M" (remove find card "^M")] to end]
parse card [some [to "^/" (remove find card "^/")] to end]
card: trim card
]
cleancard card
cardlength: length? card
cardnum: []
cardsum: 0
typecard: ""
cardvalid: false
switch: off
oldvisa: off
if (length? card) = 16 [switch: on]
if (length? card) = 13 [
if (to-integer to-string first card) = 4 [switch: on oldvisa: on]
]
if switch = on [
foreach digit card [
digit: to-integer to-string digit
append cardnum digit
]
if oldvisa = on [append cardnum 0]
foreach [one two] cardnum [
if one < 5 [cardsum: cardsum + (one * 2)]
if one > 4 [
one: one * 2
one: to-string one
cardsum: cardsum + (to-integer to-string one/1 +
to-integer to-string one/2)
]
cardsum: cardsum + two
]
if cardnum/1 = 4 [typecard: "Visa"]
if cardnum/1 = 5 [
if cardnum/2 < 6 [
if cardnum/2 > 0 [typecard: "MasterCard"]
]
]
if (remainder cardsum 10) = 0 [cardvalid: true]
if (remainder cardsum 10) <> 0 [cardvalid: false]
]
]
;;;;;;;;;;;;;;;; end credit card validation function ;;;;;;;;;
card: " <<enter a credit card number here>> "
validate card
print [cardnum cardlength cardsum typecard cardvalid]
[2/4] from: ralph:abooks at: 17-Sep-2000 18:04
by the way, this only works for Visa and MasterCard; I'll add Diner's Club,
Amex, etc. later.
--Ralph
[3/4] from: g:santilli:tiscalinet:it at: 18-Sep-2000 19:02
Hello [ralph--abooks--com]!
On 16-Set-00, you wrote:
r> cleancard: func [card][
r> replace/all card " " ""
r> parse card [some [to "^M" (remove find card "^M")] to end]
r> parse card [some [to "^/" (remove find card "^/")] to end]
r> card: trim card
r> ]
I think that:
trim/with card " ^/^M"
should be faster. Also,
to-integer card/1 - 48
is a bit faster than:
to-integer to-string card/1
Regards,
Gabriele.
--
Gabriele Santilli <[giesse--writeme--com]> - Amigan - REBOL programmer
Amiga Group Italia sez. L'Aquila -- http://www.amyresource.it/AGI/
[4/4] from: ralph:abooks at: 18-Sep-2000 16:41
ah zo! Thanks, Gabriele!