Click Here!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Platinum Edition Using HTML 4, XML, and Java 1.2
(Publisher: Macmillan Computer Publishing)
Author(s): Eric Ladd
ISBN: 078971759x
Publication Date: 11/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


Validation Using the Luhn Algorithm

The algorithm used to validate the credit card number is known as the Luhn Algorithm, and it employs the following steps:

1.  Double the value of alternating digits of the credit card number, beginning with the second digit from the right.
Example: For the number 1234 5678 1234 5678
 1  2  3  4  5  6  7  8  1  2  3  4  5  6  7  8
×2    ×2    ×2    ×2    ×2    ×2    ×2    ×2
-------------------------------------------------
 2     6    10    14     2     6    10    14
2.  Add the separate digits of the products found in step 1 (10 yields the separate digits 1 and 0) along with all the credit card digits not used in step 1.
Example: Products = 2+6+(1+0)+(1+4)+2+6+(1+0)+(1+4) = 28
Unaffected Digits = 2+4+6+8+2+4+6+8 = 40
Total = 28 + 40 = 68

The total found by following the preceding steps must be divisible by 10 (must end in 0) for it to be a well-formed credit card number.

Example: Total = 68. This number is not well formed.

If the original number in the preceding example had been

1234 5678 1234 5670

the total would have worked out to be 60, and it would be well formed. This number would not have passed the complete well-formed test, however, because its prefix and length do not match any of the major credit cards shown in Table 21.1.

If you are planning on using JavaScript functions to verify that credit card numbers are well formed, you should have a set of sample numbers to use to verify that the functions are working correctly. (It is not a good idea to test these functions by using your own credit card numbers unless you are sure their information won’t be accessible over the Web.) Table 21.2 shows a set of sample well-formed credit card numbers, available from the Netscape Web site. (The next section of this chapter discusses this Netscape site in more detail.)

Table 21.2—Sample Well-Formed Credit Card Numbers

Card Type Sample Number

MasterCard 5500 0000 0000 0004
Visa 4111 1111 1111 1111
American Express 3400 0000 0000 009
Diner’s Club or Carte Blanche 3000 0000 0000 04
Discover 6011 0000 0000 0004
enRoute 2014 0000 0000 009
JCB 3088 0000 0000 0009

A number of freely available JavaScripts implement the test for a well-formed credit card number. You can find one of them, developed by Simon Tneoh, at the following URL:

http://www.tneoh.zoneit.com/javascript/cardobject.html

A slightly altered version (to fix some minor typographical mistakes) of the CardType.js JavaScript source file available from this Web site is included with the accompanying CD-ROM. You can find it in the file named ChkCard.js. Listing 21.8 shows ChkCard.htm, an HTML document that makes use of this JavaScript source file to implement its credit card number tests.

Listing 21.8 ChkCard.htm —Checking for Well-Formed Credit Card Numbers Can Be the First Step in Validating Them; You Still Need to Verify Them on the Server


<HTML>
<HEAD>
<TITLE>Check for Well-Formed Credit Card Number</TITLE>
<SCRIPT LANGUAGE="JAVASCRIPT" src="ChkCard.js"></SCRIPT>
</HEAD>
<BODY BGCOLOR=#FFFFFF>
<CENTER>
<H1>Check for Well-Formed Credit Card Number</H1>
<HR>
</CENTER>
<FONT COLOR=#FF0000>Alert!
Please do not use a real card number and valid expiration date
to test. If you really want to do it, please check the source
first, make sure it will not submit the card number.
</FONT>
<CENTER>
<TABLE>
<FORM NAME="MyForm">
<TR><TD>Card Number:</TD>
    <TD><INPUT TYPE=TEXT NAME="CardNumber" SIZE=16 MAXLENGTH=19></TD></TR>
<TR><TD>Card Type:</TD>
    <TD><SELECT NAME="CardType">
           <OPTION VALUE="MasterCard">MasterCard
           <OPTION VALUE="VisaCard">Visa
           <OPTION VALUE="AmExCard">American Express
           <OPTION VALUE="DinersClubCard">Diners Club
           <OPTION VALUE="DiscoverCard">Discover
           <OPTION VALUE="enRouteCard">enRoute
           <OPTION VALUE="JCBCard">JCB
           <OPTION VALUE="LuhnCheckSum">Luhn Check Only
        </SELECT></TD></TR>
<TR><TD>Expiration Month:</TD>
    <TD><SELECT NAME="ExpMon">
           <OPTION VALUE=01>January
           <OPTION VALUE=02>February
           <OPTION VALUE=03>March
           <OPTION VALUE=04>April
           <OPTION VALUE=05>May
           <OPTION VALUE=06>June
           <OPTION VALUE=07>July
           <OPTION VALUE=08>August
           <OPTION VALUE=09>Septembet
           <OPTION VALUE=10>October
           <OPTION VALUE=11>November
           <OPTION VALUE=12 SELECTED>December
        </SELECT></TD></TR>
<TR><TD>Expiration Year:</TD>
    <TD><SELECT NAME="ExpYear">
           <OPTION VALUE=97 selected>1997
           <OPTION VALUE=98>1998
           <OPTION VALUE=99>1999
           <OPTION VALUE=00>2000
           <OPTION VALUE=01>2001
           <OPTION VALUE=02>2002
        </SELECT></TD></TR>
<TR><TD>&nbsp;</TD>
    <TD><INPUT TYPE=BUTTON VALUE="Check"
               OnClick="CheckCardNumber(document.MyForm)"></TD></TR>
</FORM>
</TABLE>
<HR>
</CENTER>
<ADDRESS>
Jim O’Donnell, <A HREF="mailto:odonnj@rpi.edu">odonnj@rpi.edu</A>
</ADDRESS>
</BODY>
</HTML>


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.