by the way, this only works for Visa and MasterCard; I'll add Diner's Club,
Amex, etc. later.
--Ralph
>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]
>