[thelist] Credit Card Check Digit Validation

Keith cache at dowebs.com
Tue Jan 29 02:12:00 CST 2002


> On one of evolt's pages http://dir.evolt.org/tutorials/articles/ there
> is a link to http://www.beachnet.com/~hstiles/cardtype.html which
> shows how to do preliminary validation of the input in a credit card
> number field.
> My question is... does anyone know if this has changed OR does any one
> know of more recent documentation on the subject.

The mod-10 Luhn test refered to in that article is unlikely to change for a
long time. Credit card numbers have been constructed on the mod-10 zero
sum algorithm since VISA was known as BankAmeriCard and
MasterCard was known as Master-Charge. In addition to websites testing
for valid number formats, most, if not all, swipe machines also do a "client-
side" validation before connecting with the call centers to see if the card
was properly swiped. That test is hard coded on prom chips. This Luhn
algorithm (patented in 1960) is also used for bar code scanners, it's how
the clerk knows that the scan didn't take. I've read that phone cards also
use it.

If you search Google with "luhn credit card test" or "mod10 credit card
test" you'll find plenty of up-to-date articles ranging from javascript
validators to the character-by-character architecture of a card number.

In a nutshell, "For a card with an even number of digits, double every odd
numbered digit and subtract 9 if the product is greater than 9. Add up all
the even digits as well as the doubled-odd digits, and the result must be a
multiple of 10 or it's not a valid card. If the card has an odd number of
digits, perform the same addition doubling the even numbered digits
There are a number of validation routines available online in javascript,
perl, php and others. But, most of them are overkill, the only thing a test
must do is validate the algorithm, it doesn't need to know the type of card
or anything else (you _can_ extract the issuing bank if you really want to).
<tip type="Remove Duplicates - Perl" author="keith">
Suppose you have a large array and you want to remove all duplications,
for example: you have multiple lists of email addresses that have been
merged into one list. Lets say you have the email addresses in an array
@e and you want the cleaned-up list in @E. 4 lines of code:
foreach $e (@e){
unless("@E" =~ /$e/){push (@E,$e)}
This will sort and remove duplicates for 100K+ lists.


More information about the thelist mailing list