网上搜索的看着都蛋疼。实现的太复杂,故精简如下:

1
2
3
4
5
6
7
8
9
10
11
function checkBankCard(cardNo) {
var tmp = true,
total = 0
for (var i = cardNo.length; i > 0; i--) {
var num = cardNo.substring(i, i - 1)
if (((tmp = !tmp), tmp)) num = num * 2
var gw = num % 10
total += gw + (num - gw) / 10
}
return total % 10 == 0
}

 评论