Лучшим методом будет использование теста LUHN. https://en.wikipedia.org/wiki/Luhn_algorithm
/*
mod10(cardNumber)
parameters:
this function takes the text string card number and runs the Mod 10 formula on its respective digits.
description:
Mod 10 is the check digit formula for the supported cards these functions attempt to validate.
returns:
this function returns true if the number passes the check digit test.
false otherwise.
*/
function mod10(cardNumber) { // LUHN Formula for validation of credit card numbers.
var ar = new Array(cardNumber.length);
var i = 0,sum = 0;
for(i = 0; i < cardNumber.length; ++i) {
ar[i] = parseInt(cardNumber.charAt(i));
}
for(i = ar.length -2; i >= 0; i-=2) { // you have to start from the right, and work back.
ar[i] *= 2; // every second digit starting with the right most (check digit)
if(ar[i] > 9) ar[i]-=9; // will be doubled, and summed with the skipped digits.
} // if the double digit is > 9, ADD those individual digits together
for(i = 0; i < ar.length; ++i) {
sum += ar[i]; // if the sum is divisible by 10 mod10 succeeds
}
return (((sum%10)==0)?true:false);
}
Где находится «ошибка выше»? –