Post by talk2santosh on Feb 25, 2004 5:32:06 GMT -5
www.atgconsulting.com/creditcards.asp
Cheap Tricks: Fun With Credit Cards
Anybody can check a credit card number... the algorithms for generating and verifying check-digits are out there for the taking. For example, you might want to review this web site that was featured prominently in a recent article in MSDN -- www.beachnet.com/~hstiles/cardtype.html.
In today's article we're going beyond that: we'll kick it up a notch, and show you not just how to check the card number, but also how to give your visitors a visually intriguing effect -- adding value while performing the otherwise onerous task of validating an obscure multi-digit number.
Standard caveat applies -- the spiffy graphical effects works like a charm in IE, but NN sucks.
You Do the Math
Credit card numbers are a string of numeric digits from 13 to 16 characters long. Validation ignores any spaces or dashes, so it's good to get in the habit of entering numbers without them -- it makes everyone's life easier. Here are the rules for how to see if a credit card number is valid.
Starting at the rightmost position, multiply each individual digit by 1 if its position (from the right) is odd, or by 2 if its position is even.
If the product of this multiplication is greater than 9, add the individual digits of the product together. For example, if the result was 16, you would add 1 + 6 = 7. (If the result was 17, you need to go back to second grade.)
Add the resulting single-digit numbers together. If the final sum is a multiple of 10 then the credit card number is valid. If not... guess again.
For example here's a seemingly random 15 digit number... 378765981401955
position odd even odd even odd even odd even odd even odd even odd even odd
number 3 7 8 7 6 5 9 8 1 4 0 1 9 5 5
multiply 3 14 8 14 6 10 9 16 1 8 0 2 9 10 5
add 3 5 8 5 6 1 9 7 1 8 0 2 9 1 5
total 3 8 16 21 27 28 37 44 45 53 53 55 64 65 70
Which means what? It might be a valid credit card number! Hmmm... now what was that hot chat 800 number again?
But Which Kind of Credit Card Number Is It?
Now hold on thar, Bubba!
Just because a random string of digits passes the check-digit validation, that's no guarantee that it's a good number. Each of the issuing authorities assigns credit cards in a designated sequence. Here are the general rules of thumb:
Mastercard numbers are 16 digits long and start with 51, 52, 53, 54, or 55.
Visa numbers are either 13 or 16 digits long and start with a 4.
American Express numbers are 15 digits long and start with 34 or 37.
Discover card numbers are 16 digits long and start with 6011.
Diners Club numbers are 14 digits long and start with 30, 36, or 38,
(and almost nobody takes them -- especially on the web).
One interesting side-effect of this knowledge is that now you know, you don't ever have to ask someone what type of a credit card it is... the number automatically tells you what type of card it is. Since so many people have hard-coded these algorithmic sequences, when one of the issuers runs out of numbers in their designated sequence, I suppose the world will just come to an end in a cataclysm of Y2K-ish proportions. So don't be afraid to join up now...
The Effect
For fun, type a credit card number into this textbox... (If you don't trust me, or if your mommy told you not to give your credit card to strangers unless you see the little "security" icon in the corner... but you don't want to miss out on something spiffy and cool... you could try typing in the test number "4111111111111111".)
When you do, if it's a valid number, and if you're running IE, a picture of the credit card should show up in the little box.
How it works is, we've hooked the "ONKEYUP" event of the textbox to a little client-side javascript that checks the number you entered... and returns the credit card name. But as per usual, NN sucks, because they don't respond to keystrokes -- they only respond when you leave the field, in the ONCHANGE event. As a sop... we threw that little "?" icon on the page... it doesn't really do anything, but it forces the "onchange" event to fire.
Cheap Tricks: Fun With Credit Cards
Anybody can check a credit card number... the algorithms for generating and verifying check-digits are out there for the taking. For example, you might want to review this web site that was featured prominently in a recent article in MSDN -- www.beachnet.com/~hstiles/cardtype.html.
In today's article we're going beyond that: we'll kick it up a notch, and show you not just how to check the card number, but also how to give your visitors a visually intriguing effect -- adding value while performing the otherwise onerous task of validating an obscure multi-digit number.
Standard caveat applies -- the spiffy graphical effects works like a charm in IE, but NN sucks.
You Do the Math
Credit card numbers are a string of numeric digits from 13 to 16 characters long. Validation ignores any spaces or dashes, so it's good to get in the habit of entering numbers without them -- it makes everyone's life easier. Here are the rules for how to see if a credit card number is valid.
Starting at the rightmost position, multiply each individual digit by 1 if its position (from the right) is odd, or by 2 if its position is even.
If the product of this multiplication is greater than 9, add the individual digits of the product together. For example, if the result was 16, you would add 1 + 6 = 7. (If the result was 17, you need to go back to second grade.)
Add the resulting single-digit numbers together. If the final sum is a multiple of 10 then the credit card number is valid. If not... guess again.
For example here's a seemingly random 15 digit number... 378765981401955
position odd even odd even odd even odd even odd even odd even odd even odd
number 3 7 8 7 6 5 9 8 1 4 0 1 9 5 5
multiply 3 14 8 14 6 10 9 16 1 8 0 2 9 10 5
add 3 5 8 5 6 1 9 7 1 8 0 2 9 1 5
total 3 8 16 21 27 28 37 44 45 53 53 55 64 65 70
Which means what? It might be a valid credit card number! Hmmm... now what was that hot chat 800 number again?
But Which Kind of Credit Card Number Is It?
Now hold on thar, Bubba!
Just because a random string of digits passes the check-digit validation, that's no guarantee that it's a good number. Each of the issuing authorities assigns credit cards in a designated sequence. Here are the general rules of thumb:
Mastercard numbers are 16 digits long and start with 51, 52, 53, 54, or 55.
Visa numbers are either 13 or 16 digits long and start with a 4.
American Express numbers are 15 digits long and start with 34 or 37.
Discover card numbers are 16 digits long and start with 6011.
Diners Club numbers are 14 digits long and start with 30, 36, or 38,
(and almost nobody takes them -- especially on the web).
One interesting side-effect of this knowledge is that now you know, you don't ever have to ask someone what type of a credit card it is... the number automatically tells you what type of card it is. Since so many people have hard-coded these algorithmic sequences, when one of the issuers runs out of numbers in their designated sequence, I suppose the world will just come to an end in a cataclysm of Y2K-ish proportions. So don't be afraid to join up now...
The Effect
For fun, type a credit card number into this textbox... (If you don't trust me, or if your mommy told you not to give your credit card to strangers unless you see the little "security" icon in the corner... but you don't want to miss out on something spiffy and cool... you could try typing in the test number "4111111111111111".)
When you do, if it's a valid number, and if you're running IE, a picture of the credit card should show up in the little box.
How it works is, we've hooked the "ONKEYUP" event of the textbox to a little client-side javascript that checks the number you entered... and returns the credit card name. But as per usual, NN sucks, because they don't respond to keystrokes -- they only respond when you leave the field, in the ONCHANGE event. As a sop... we threw that little "?" icon on the page... it doesn't really do anything, but it forces the "onchange" event to fire.