This is how I did it with Luhn-algorithm and card number length (we only support specific cards that have 16 digits):
// Luhn-algorithm check
function checkCardNumber(number) {
var sumTable = [
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[0, 2, 4, 6, 8, 1, 3, 5, 7, 9]
],
sum = 0,
flip = 0,
i;
for (i = number.length - 1; i >= 0; i--) {
sum += sumTable[flip++ & 0x1][parseInt(number.charAt(i), 10)];
}
return sum % 10 === 0;
}
// Check if the VISA card input is valid
var regCardNo = (opts.cardData ? /^[0-9]{6}[\*]{6}[0-9]{4}$/ : /^[0-9]{16}$/),
regMonth = /^[0]{1}[1-9]{1}$|^[1]{1}[0-2]{1}$/,
regYear = /^[0]{1}[1-9]{1}$|^[1-9]{1}[0-9]{1}$/,
regCvc = /^[0-9]{3}$/,
cardNo = cardNumberInput.val(),
month = monthInput.val(),
year = yearInput.val(),
cvc = cvcInput.val();
if(regCardNo.test(cardNo) && regMonth.test(month) && regYear.test(year) && regCvc.test(cvc) && checkCardNumber(cardNo)) {
payButton.prop("disabled", false);
return true;
} else {
payButton.prop("disabled", true);
return false;
}
All input variables are jQuery elems (they are not defined in this code snippet )
NOTE that the opts.cardData checks if you have a prefilled card number (which gets loaded from the DB), then it checks for a masked number (ex. 123456******1234), else it checks for a full length number which you add in the form your self.