Everything2
Near Matches
Ignore Exact
Full Text
Everything2

credit card validation

created by eric+

(idea) by eric+ (5.6 y) (print)   ?   (I like it!) 1 C! Fri Jul 21 2000 at 5:35:38

Validating a credit card number (the check sum bit, I mean. The having sufficient credit part you need to figure out yourself.) is a really simple process. You can practically do it in your head.

For example:

  • 4111 1111 1111 1111
  • 1111 1111 1111 1114
  • 1212 1212 1212 1218
  • 1 + 2 + 1 + 2 + 1 + 2 + 1 + 2 + 1 + 2 + 1 + 2 + 1 + 2 + 1 + 8 = 30
  • 30 mod 10 = 0

Here's a little perl subroutine to do it for ya. Returns 1 for valid and 0 for invalid.

    sub is_valid_cc {
        my $cc = shift() || return 0;
     
        my @digits = reverse split( '', $cc );       # reverse the digits
        for ($i=0; $i < scalar(@digits); $i++) {                          
            $digits[$i] *= ($i%2) ? 2 : 1;           # double the evens
        }
        my $string = join( '', @digits );            # recombine into a string

        my $sum = 0;
        foreach ( split( '', $string ) ) {           # add the digits
            $sum += $_;
        }
         
        return ($sum % 10) ? 0 : 1;
    }                              


heropsychodreamer : the reason you can't just double the odd digits is that that algorithm will break if the credit card number has an odd number of digits (like American Express and old Visa credit cards).

printable version
chaos

The sum of an infinite series of positive numbers is negative What really happens while the credit agency's phone is ringing Mod Mastercard
Perl CVV2/CVC2/CID double-entry bookkeeping VISA
double valid simple invalid
American Express Card Jetton letter of credit free
Tallit American Express Remember to include ALL your e-mail addresses marble
Too much bandwidth Tesa Equifax Reverse
Y'know, if you log in, you can write something here, or contact authors directly on the site. Create a New User if you don't already have an account.
  Epicenter
Login
Password

password reminder
register

Everything2 Help

Cool Staff Picks
Things you could have written:
How can a good Buddhist work in advertising?
Watching Karen laugh
Mainstreaming Companion
Calculating God
The Conversation
asthma
love like a knife
How to entertain young children on little or no money
Oolong
I Believe in Everything
Service for those Fallen Asleep
Rockall
Create a universe
New Writeups
Aerobe
Watch out for falling meat(poetry)
C-Dawg
Beelzebub has a devil put aside for me(fiction)
Pavlovna
My Better Half(fiction)
kanoodle
Molson muscle(essay)
aneurin
You pays your money and you takes your choice(idea)
shaogo
July 20, 2008(log)
Glowing Fish
Tualatin River(place)
The Jacket
Words of Advice(idea)
John_Fox
Good Intentions Gone Wrong(person)
Heitah
Posthumous Oscar(thing)
ignis_glaciesque
University of South Florida(place)
ignis_glaciesque
Flogstaskriket(idea)
liveforever
Caesar's last breath(idea)
dagnyswaggart
she wants to believe(personal)
antigravpussy
he doesn't know, but her eyes widen too far(thing)
This page courtesy of The Everything Development Company