javascript credit card number validation function

// Check credit card number is correct using Luhn Mod-10 test

function check_cc(field_name, card_type_field_name, card_type) {

if ((field_name.value == “”) || (field_name.value.length < 14) ) {

field_name.focus();

alert(“\”Credit Card number\” appears to be blank or incorrect length, please check and re-enter”);

return (false);

}

var st = field_name.value;

var ccStr = “”;

whitespace = ” \t\n\r”;

for (i = 0; i < st.length; i++) {

var c = st.charAt(i);

if (whitespace.indexOf(c) == -1) ccStr += c;

}

if (ccStr.length > 19) allValid = false;

sum = 0; mul = 1; l = ccStr.length;

for (i = 0; i < l; i++) {

digit = ccStr.substring(l-i-1,l-i);

if (document.layers)

tproduct = parseInt(digit)*mul;

else

tproduct = parseInt(digit ,10)*mul;

if (tproduct >= 10)

sum += (tproduct % 10) + 1;

else

sum += tproduct;

if (mul == 1)

mul++;

else

mul–;

}

if ((sum % 10) == 0)

allValid = true;

else

allValid = false;

if (!allValid) {

field_name.focus();

alert(“\”Credit Card Number\” appears to be invalid, please check and re-enter”);

return (false);

}

if (card_type == ‘A’) {

allValid = true;

if (ccStr.length != 15) allValid = false;

if (ccStr.charAt(0) != ‘3’) allValid = false;

if ((ccStr.charAt(1) != ‘4’) && (ccStr.charAt(1) != ‘7’)) allValid = false

if (!allValid) {

card_type_field_name.focus();

alert(“Please check the credit card type and card number, card appears not to be an American Express card.”);

return (false);

}

}

if (card_type == ‘D’) {

allValid = true;

if (ccStr.length != 14) allValid = false;

if (ccStr.charAt(0) != ‘3’) allValid = false;

// Modified 05-Jul-01, Robert Dyball: on advice from Diners, valid range may be 30 to 39, following line not required

// if ((ccStr.charAt(1) != ‘0’) && (ccStr.charAt(1) != ‘6’) && (ccStr.charAt(1) != ‘8’)) allValid = false

if (!allValid) {

card_type_field_name.focus();

alert(“Please check the credit card type and card number, card appears not to be a Diners Club card.”);

return (false);

}

}

if (card_type == ‘B’) {

allValid = true;

if (ccStr.length != 16) allValid = false;

if (ccStr.charAt(0) != ‘5’) allValid = false;

if (ccStr.charAt(1) != ‘6’) allValid = false;

if (!allValid) {

card_type_field_name.focus();

alert(“Please check the credit card type and card number, card appears not to be a Bankcard.”);

return (false);

}

}

if (card_type == ‘V’) {

allValid = true;

if ((ccStr.length != 13) && (ccStr.length != 16)) allValid = false;

if (ccStr.charAt(0) != ‘4’) allValid = false;

if (!allValid) {

card_type_field_name.focus();

alert(“Please check the credit card type and card number, card appears not to be a Visa card.”);

return (false);

}

}

if (card_type == ‘M’) {

allValid = true;

if (ccStr.length != 16) allValid = false;

if (ccStr.charAt(0) != ‘5’) allValid = false;

if ((ccStr.charAt(1) < ‘1’) || (ccStr.charAt(1) > ‘5’)) allValid = false

if (!allValid) {

card_type_field_name.focus();

alert(“Please check the credit card type and card number, card appears not to be a Mastercard.”);

return (false);

}

}

return (true);

}

Advertisements