Skip to content

Instantly share code, notes, and snippets.

@jinweijie
Created August 8, 2015 06:37
Show Gist options
  • Select an option

  • Save jinweijie/327650595174113f85ae to your computer and use it in GitHub Desktop.

Select an option

Save jinweijie/327650595174113f85ae to your computer and use it in GitHub Desktop.
function isCardNumberValid(cardNumber, allowSpaces) {
if (allowSpaces) {
cardNumber = cardNumber.replace(/ /g, '');
}
if (!cardNumber.match(/^\d+$/)) {
return false;
}
var checksum = 0;
for (var i = 0; i < cardNumber.length; i++) {
var n = (cardNumber.charAt(cardNumber.length - i - 1) - '0') << (i & 1);
checksum += n > 9 ? n - 9 : n;
}
return (checksum % 10) == 0 && checksum > 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment