# Call this subroutine with the credit card number you wish to validate # (spaces and hyphens allowed). It will return 1 if the number passes the # checksum checks, 0 otherwise. Note that this does NOT go to a credit bureau # to authenticate the user's credit worthiness! # See "Web Security, A Step-by-Step Reference Guide" by Lincoln Stein, page 46 sub check_cc { my $num = shift; my $sum = 0; my @digits = $num=~/(\d)/g;; my @weights = (1,2) x (@digits/2 + 1); shift @weights unless @digits % 2; foreach (@digits) { my $weighted = $_ * shift @weights; $weighted -= 9 if $weighted > 9; $sum += $weighted; } return ($sum % 10) == 0; }