WebReference.com - Chapter 10 selections: Practical JavaScript for the Usable Web from glasshaus (5/5). | WebReference

WebReference.com - Chapter 10 selections: Practical JavaScript for the Usable Web from glasshaus (5/5).

To page 1To page 2To page 3To page 4current page
[previous]

Practical JavaScript for the Usable Web

In the table below, we show what digits are extracted and what values added to the running total for the example card number 4221 3456 1243 1287.

Character ExtractedWhat happens to itWhat happens to running totalRunning total Value
7Added to running totalRunning total (0) + character extracted (7)7
8Doubled (16), then each digit in the product added to running totalRunning total (7) + first digit in product (1) + second digit in product (6)14
2Added to running totalRunning total (14) + character extracted (2)16
1Doubled (2), then each digit in the product added to running totalRunning total (16) + first digit in product (2)18
3Added to running totalRunning total (18) + character extracted (3)21
4Doubled (8), then each digit in the product added to running totalRunning total (21) + first digit in product (8)29
2Added to running totalRunning total (29) + character extracted (2)31
1Doubled (2), then each digit in the product added to running totalRunning total (31) + first digit in product (2)33
6Added to running totalRunning total (33) + character extracted (6)39
5Doubled (10), then each digit in the product added to running totalRunning total (39) + first digit in product (1) + second digit in product(0)40
4Added to running totalRunning total (40) + character extracted (4)44
3Doubled (6), then each digit in the product added to running totalRunning total (44) + first digit in product (6)50
1Added to running totalRunning total (50) + character extracted (1)51
2Doubled (4), then each digit in the product added to running totalRunning total (51) + first digit in product (4)55
2Added to running totalRunning total (55) + character extracted (2)57
4Doubled (8), then each digit in the product added to running totalRunning total (57) + first digit in product (8)65

Once we have our result, in the table above that's 65, we then find its modulus 10 value, that is the remainder left over when the running total is divided by ten. If it's zero, we have ourselves a valid credit card number, otherwise it's a fake. We set isValid to the result of the Boolean expression comparing the remainder to zero-–if true it's valid, if false it's invalid. In our example, we have 65, the modulus 10 value of which is 5, so the example number is invalid-–you didn't really think I'd give my credit card number out now did you?

[Editor's note--UPDATE, 4/19/02: As originally published (both here and in the actual book) the function described in this excerpt was flawed, in that it did not add the last (left-most) digit of the credit card number into the checksum for card numbers with an odd number of digits (i.e., AmEx, Visa with 13 digits). As originally published, the outer loop in the script read:

     for (digitCounter = cardNumberLength - 1;
       digitCounter > 0;
       digitCounter--)
     {

2 credit card digits were processed with each pass of the outer loop. On a 15 digit card, the loop begins with 14 (the last index digit in a 15 number card), then 12 on the next pass, then 10, 8, 6, 4, and 2. The final pass of the loop that would then add in the first digit of the card (at index position 0) never occurs; since the for loop is specifically tested for digitCounter > 0.

Of course, multiple fixes are possible. The publisher suggests altering the loop to check for digitCounter >= 0, which is what is now included in this article. However, as this can result in NaN problems with some platforms (we encountered errors when testing in Opera 6 and in early versions of NS4.x) when attempting to multiply an empty string by a number, we suggest the following minimal correction:

     for (digitCounter = cardNumberLength - 1;
       digitCounter >= 0;
       digitCounter--)
     {
       checkSumTotal += parseInt (cardNumbersOnly.charAt(digitCounter));
       if (digitCounter > 0) 
       }
          digitCounter--;
          numberProduct = String((cardNumbersOnly.charAt(digitCounter) * 2));
          for (var productDigitCounter = 0;
            productDigitCounter < numberProduct.length;
            productDigitCounter++)
          {
            checkSumTotal +=
              parseInt(numberProduct.charAt(productDigitCounter));
          }
       }
     }

Our thanks to Jez Brett for being the first to point out the error.]

To page 1To page 2To page 3To page 4current page
[previous]

Created: April 15, 2002
Revised: April 19, 2002


URL: http://webreference.com/programming/javascript/practical/chap10/5.html