Credit Card Check Validation Routine
CSGNetwork FREE Source Code, Javascript Source, and Code Snippets!
CSGNetwork Custom Search
     Bookmark and Share
Top Free Apps

Right click this window and select "view source" in order to copy the source for this script.

Credit Card Number:

This script is designed to validate using Javascript's client capabilites that the number that is entered COULD possibly be a valid credit card number.

Credit card numbers are all numeric and have fixed lengths (13 digits for Diners Club, 15 for American Express, and 16 digits for all the others). Some other credit card numbering systems vary. The card numbers also contain what is called a "check digit" to help ensure that if you transpose digits in the entered credit card number that you are not accidentally quoting a number belonging to someone else. This means that only certain number combinations are valid.

What you can do with Javascript is to test the number that is entered to see if it could be a valid credit card number. The following field will test a card number for appropriate length as well as confirming that the number has a valid "check digit". (It also restricts the field input to numeric). The actual values used in some of the positions within the number also have special meanings (such as card issuer, country of issue, etc.) but this routine does not check for those. There are still many possible invalid card numbers that would be accepted by this routine and would not be picked up until later but the script does reject 90% of all possible number combinations (all valid numbers are within the remaining 10% that the script lets through).

Enter something into the above credit card validation test field; then click the validate button. An alert box will display to tell you whether the number entered COULD be a valid credit card number. You can apply this validation to your own credit card number fields by calling the following validation routine from the point in your code you wish to perform the validation. Pass the field to be validated (non-numerics in the field will be ignored). The function will return true if the number supplied is the correct length and COULD be a credit card number; otherwise, it will return false if it is definitely not a valid card number.

There are limitations. This routine only tests if the number entered is the right length to be a credit card number and contains the correct check digit. Numbers accepted by this code COULD be valid credit card numbers but may not actually correspond to a card that has been issued. Cards rejected by the routine are definitely not valid. No cards are tested for credit capability in this routine. To properly validate credit card numbers requires server side processing but this code will help you to reject many invalid numbers before the server side routine needs to be called. Note also that this script handles preliminary validation only and does not contain any code relating to security. You need to have appropriate security measures (including possible encryption) in place if you are going to ask for people to enter their credit card numbers on your site.

Bookmark and Share
Registered® Trademark™ and Copyrightę 1973 - CSG, Computer Support Group, Inc. and CSGNetwork.Com All Rights Reserved