hpadwal,
I agree with kaht that your validation should be done server-side but still think you would benefit from client-side validation as well. The point being that you want to verify that the fied info is at least of a valid type prior to allowing the form to submit. You still need to validate server-side but at least you are not posting the form then having to re-display the form due to errors.
Here is some code I have been using.
It validates most types of credit cards but whether it works on the ones you specify above I could not say.
Code:
function credit_card(s)
{
var i, n, c, r, t;
// First, reverse the string and remove any non-numeric characters.
r = "";
for (i = 0; i < s.length; i++) {
c = parseInt(s.charAt(i), 10);
if (c >= 0 && c <= 9)
r = c + r;
}
// Check for a bad string.
if (r.length <= 1)
return false;
// Now run through each single digit to create a new string. Even digits are multiplied by two, odd digits are left alone.
t = "";
for (i = 0; i < r.length; i++) {
c = parseInt(r.charAt(i), 10);
if (i % 2 != 0)
c *= 2;
t = t + c;
}
// Finally, add up all the single digits in this string.
n = 0;
for (i = 0; i < t.length; i++) {
c = parseInt(t.charAt(i), 10);
n = n + c;
}
// If the resulting sum is an even multiple of ten (but not zero), the card number is good.
if (n != 0 && n % 10 == 0)
return true;
else
return false;
}
This code works for MasterCard 13 and 16 digit numbers, Visa, American Express & Amex Corporate (15 digit), Diner's Club (14 digit), Discover and JCB.
Here are some test numbers to verify the script with.
Master Card 5431-1111-1111-1111 (16) Characters
Master Card 4222222222222 (13) Characters
VISA 4111111111111111 (16) Characters
VISA 4012888888881881 (16) Characters
American Express 341111111111111 (15) Characters
Amex Corporate 378734493671000 (15) Characters
Dinners Club 38520000023237 (14) Characters
Discover 6011111111111117 (16) Characters
Discover: 6011-6011-6011-6611 (16) Characters
JCB 3530111333300000 (16) Characters
Stamp out, eliminate and abolish redundancy!