Validate a possible credit card number.
Card numbers are designed with a 1-in-10 checksum; if someone makes an error in entering the number, this convenience can usually detect and avoid extra calls--"Well, your card isn't working; can we check the number again?"
It is not designed to validate a card account--don't give merchandise away based on a card number that merely passes this check. All to be done to trick someone with a "valid" card number is to try changing the last digit.
Note: Most business don't accept all types of credit cards. The different cards are grouped by their beginning number: 4 = VISA, 5 = MC, 6 = Discover. (Inform me of others.)
Since checksum only checks for entry errors, you may need to make sure the type of credit card is acceptable (one of the kinds your company can take) by running a function something like this:
function cardtypeOK(x) { // x is the number as text, like "123423..."
var a='456', i;
for (i=0; i<a.length; i++) if (x.charAt(0) == a.charAt(i)) break;
if (i == a.length) return false;
return true;
}
where '456' are the beginning digits of credit cards your business can accept (in this case visa, mc, and discover).