// The function determines whether a Credit Card number is "valid"
// Please note that a "valid" Credit Card number is not essentially a Credit Card in "Good Standing"
// Please note that a "valid" Credit Card number is not essentially a Credit Card in "Good Standing"
function isValidCreditCard(number) {
if (number.indexOf("-")) {
cc = number.split("-");
number = "";
for (var i = 0; i < cc.length; i++) number += cc[i];
}
if (number.indexOf("-")) {
cc = number.split("-");
number = "";
for (var i = 0; i < cc.length; i++) number += cc[i];
}
// Another Version of what was performed above using String & Array Methods
if (number.indexOf(" ")) {
cc = number.split(" ");
number = cc.join("");
}
if (number.indexOf(" ")) {
cc = number.split(" ");
number = cc.join("");
}
// OR using RegExp we can combine the above two
// number = number.replace(/-|\s/g, "");
// number = number.replace(/-|\s/g, "");
/**********************************************/
if (number.length > 19) return (false);
sum = 0; mul = 1; l = number.length;
for (i = 0; i < l; i++) {
digit = number.substring(l - i - 1, l - i);
tproduct = parseInt(digit, 10) * mul;
if (tproduct >= 10) sum += (tproduct % 10) + 1;
else sum += tproduct;
if (mul == 1) mul++;
else mul--;
}
digit = number.substring(l - i - 1, l - i);
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) return (true);
else return (false);
}
else return (false);
}
No comments:
Post a Comment