Validating Credit Card Input

 Introduction.  We have already done some work with validating inputs such as social security numbers and e-mail addresses.  Most of this was based on using particular built-in JavaScript string manipulation functions and knowing things about the formats of the desired inputs.  Now we want to move onto something that is probably much more sophisticated than you are aware, validating credit card inputs. Based on ANSI X4.13, the LUHN formula (also known as the modulus 10 -- or mod 10 -- algorithm ) is used to generate and/or validate and verify the accuracy of credit-card numbers.  Most credit cards contain a check digit, which is the digit at the end of the credit card number.  The first part of the credit-card number identifies the type of credit card (Visa, MasterCard, American Express, etc.),  The middle digits identify the bank and customer.  Algorithm for Checking Credit Cards.  To generate the check digit, the LUHN formula is applied to the number. To validate the credit-card number, the check digit is figured into the formula.  Here's how the algorithm works for verifying credit cards; the math is quite simple:  Starting with the second to last digit and moving left, double the value of all the alternating digits.   Starting from the left, take all the unaffected digits and add them to the results of all the individual digits from step 1. If the results from any of the numbers from step 1 are double digits, make sure to add the two numbers first (i.e. 18 would yield 1+8). Basically, your equation will look like a regular addition problem that adds every single digit.  The total from step 2 must end in zero for the credit-card number to be valid.  The LUHN formula was created in the late 1960s by a group of mathematicians. Shortly thereafter, credit card companies adopted it.  Because the algorithm is in the public domain, it can be used by anyone.  The LUHN formula is also used to check Canadian Social Insurance Number (SIN) validity. In fact, the LUHN formula is widely used to generate the check digits of many different primary account numbers. Almost all institutions that create and require unique account or identification numbers use the Mod 10 algorithm. An Example.  Now we are going to work an example for what might actually be a credit card number.  Consider, 6011-0065-7301-3021   Which should be somewhat reminiscent of a Discover Card number.  Let's put this number into a table that enumerates the digits.

 Place 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Digit 6 0 1 1 0 0 6 5 7 3 0 1 3 0 0 1

 Doubling 12 2 0 12 14 0 6 0

 Single Digit 3 2 0 3 5 0 6 0

 Unaffected 0 1 0 5 3 1 0 1

 Sum = 30 3 0 2 1 0 0 3 5 5 3 0 1 6 0 0 1

 So this would be a valid credit card number, which is not likely to be something that credit card companies would like to have you be able to generate. Some More Criteria.  There are also some criteria that need to be checked in order to ensure that certain types of cards have certain lengths and prefixes or leading digits.  The basic criteria are listed in the following table.

 Card Type Length Prefixes American Express 15 34, 37 Discover 16 6011 Mastercard 16 51, 52, 53, 54, 55 Visa 13 or 16 4

 Some Code.  Yikes!  So you think it would be a good assignment to have you develop the code to validate this?  Now we are going to develop a form so that you can test the validity of credit card numbers. The code has three major sections Obtaining inputs and some preprocessing Validating the coordination between type, length and prefixes Testing the Luhn criterion You should copy the following code into a file called IsCreditCard.html.

 Validating Credit Card Inputs

Validating Credit Card Inputs

Please enter the credit card number:

Check the credit card type: American Express

Discover

MasterCard

Visa

 There are a few alert( ) functions in the code to give you feedback about what has happened at particular steps.After uploading and accessing this page and entering your name, you should see something like the following at some step in the process.