#!/usr/bin/perl # Credit card numbers are not random and most errors in typing # the credit card number can be detected before contacting the server. # This is a simple perl script to check if there is no typo in the number. # It is trivial to convert this script to a JavaScript and do it within # a browser. # # If you want to try it, do: # # 1) save this script on your UNIX computer as: # credit_card_num_validation.pl # # 2) Type command: # which perl # # If your answer is different than /usr/bin/perl, change top line # to point to the correct location of your perl interpreter. # If you do not have perl installed, I cannot help you, and you # are probably beyond helping, anyhow... # # 3) Change permissions of the script to executable: # chmod 755 credit_card_num_validation.pl # # 4) Run the script with a credit card number, for example: # ./credit_card_num_validation.pl 4837609382763 # # (obviously this is not a good credit card number). # # The script was adapted from: http://en.wikipedia.org/wiki/Luhn_algorithm # Of course, the answer: "credit card is fine" does not mean that # you can use this credit card number for anything. # # Jan (I am afraid to put my last name on this piece of junk). use strict; use warnings; my $card_number = $ARGV[0] || die "Usage: $0 credit_card_number\n"; $card_number =~ s/[^0-9]//g; if(not $card_number) { die "Enter a number!!!\n"; } my @digits = split('', $card_number); my $n_digits = scalar(@digits); my $sum = 0; my $alt = 0; for (my $i = $n_digits-1; $i >= 0; $i-- ) { if ($alt) { $digits[$i] *= 2; if($digits[$i] > 9) { $digits[$i] -= 9; } } $sum += $digits[$i]; $alt = not $alt; } if( ($sum % 10) == 0 ) { print STDOUT "Card number is fine\n"; } else { print STDOUT "Card number is wrong\n"; }