Credit Card Validation

It is not possible to use Javascript to check that a credit card number that someone enters into your form belongs to them or that the card number has even been issued to anyone since these tests require access to databases containing valid credit card details at the issuer's web site (assuming that they make it available at all) and you can't perform server side validations using Javascript. What you can validate using Javascript is that the number that is entered might 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). The card numbers also contain what is called a "check digit" to help ensure that if you transpose digits in yor 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 the script that I am providing here does not check for these so there are still many possible invalid card numbers that would be accepted by this script 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).

Credit Card Number:

If you enter something into the above field then when you select 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 where ever in your code you want to perform the validation, just 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 or false if it is definitely not a valid card number:


Note that 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, ones rejected by the routine are definitely not valid. 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 PCI compliance) in place if you are going to ask for people to enter their credit card numbers on your site.

This script is provided to assist webmasters to provide preliminary validation of credit card numbers entered on their site. The level of validation of credit card numbers provided by this script is also available via similar scripts available from various Javascript sites, Felgall Pty Ltd therefore accepts no responsibility for any illegal use.

Felgall Pty Ltd accepts no responsibility for the security of credit card numbers entered on web pages that use this preliminary validation script.


This article written by Stephen Chapman, Felgall Pty Ltd.

go to top

FaceBook Follow
Twitter Follow