1. Tech

Your suggestion is on its way!

An email with a link to:


was emailed to:

Thanks for sharing About.com with others!

Credit Card Validation

clr gif
Join the Discussion

Questions? Comments?

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. This script 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 provided 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).

The first step in using this script is to select the code from the text box below (there is a highlight all button beneath it to make this easier) and copy it into a file called ccard.js.

You next attach the script into your web page that has forms containing email addresses by adding the following code into the head section of your page.

<script type="text/javascript" src="ccard.js">

All that remains then is to call it using the following statement from either the onblur event of the credit card field itself and / or the onclick or onsubmit event of the submission button or form:

valid_card =

The credit card number to be validated needs to be passed in place of cc_field in the above code.

If you prefer you can incorporate the call into an if statement processing the appropriate code based directly on the value returned instead of having to test the valid_card field. For example you could place the following into your validation code:

if (!validateCreditCard(ccfield)) {
alert('credit card number is invalid or was not entered');
return false;}

The following is a working example of the email validation script:

Credit Card Number:

While this form does not retain the entered value once you leave this page, we accept no responsibility for the security of any real credit card number that you might enter into the above form. We also accept no responsibility for the security of credit card details entered into forms where you use this validation script, that's your responsibility.

Note that the script and the description of how to use it are copied from the "Ask Felgall" website with the permission of the copyright owner.

©2017 About.com. All rights reserved.