WillMaster Possibillites Logo EzineSeek Award
Validating Credit Card Numbers On-The-Fly
by
Mari Bontrager

Permission is granted to reprint this article in its entirety, provided no reprints are sent in conjunction with unsolicited bulk email, provided no fee or other value is exchanged, provided no changes are made to the article, and provided the author's name, signature lines, and copyright line are printed with the article; except you may change the article's title.

If you collect credit cards on your site, the JavaScript presented with this article can save a lot of frustration.

  • If you collect and send credit card information to a payment gateway:

    The JavaScript can spot typographical errors before connecting your customer to the gateway. If your customer types a "7" instead of a "6," for example, the JavaScript will know something is wrong. You can allow your customer to correct the credit card number before getting a "credit card declined" message.

  • If you collect and store credit card information on your secure server for later processing:

    The JavaScript can spot typographical errors in the credit card number before you try to process it. Correcting the number right then and there can save you the time otherwise spent trying to contact the customer to get the right number.

Note: The JavaScript does not test whether or not the credit card itself is good. It just tests the number.

The JavaScript uses the Mod-10 algorithm to validate credit card numbers. It can also identify credit card types (MC, Visa, and so forth).

The Mod-10 algorithm is a special method of adding up the digits of a credit card number (reverse the order of the digits, multiply every other digit by 2, add together all of the resulting digits) and then dividing the result by 10. If the remainder is zero, the number passes. Otherwise, it fails.

The Mod-10 process is described in the article, "Creating a Credit Card Validation Class With PHP" by Mitchell Harper, available as of this writing at http://www.webmasterbase.com/article/728

The JavaScript has customization instructions. It's only four steps:

  1. What message to display if the credit card number doesn't pass the Mod-10 algorithm.

  2. What message to display if the credit card type is unknown.

  3. What message to display if the type is unknown and the Mod-10 fails.

  4. Whether or not to display a message when the credit card passes the tests.

The JavaScript is free to use from the demo page. Use your browser's "view source" to get the code or download the zip file. The URL is http://willmaster.com/a/14/pl.pl?145d

All of the JavaScript functions go into the HEAD area of your web page.

Give it a try. I think you'll find it useful.

For advanced users, the JavaScript functions provide values for three different variables. You can retrieve and use the values in your own JavaScript or CGI programs. The values are:

  1. The name of the credit card as determined by the credit card number provided.

  2. Whether or not the credit card number is of a valid format (passed the Mod-10 test).

  3. The credit card number that was checked, minus any spaces, hyphens, and other non-numeric characters the user might have typed.

More information is presented in the source code, as comments. http://willmaster.com/a/14/pl.pl?145d

By: Will Bontrager

Copyright 2002 Bontrager Connection, LLC
http://willmaster.com/possibilities/
subscribe-possibilities@willmaster.com