首页 > 代码库 > 银行卡号算法

银行卡号算法

function checkcard(banknumber) {        //取出最后一位        var last = banknumber.substr(banknumber.length-1,1);        //前15或18位        var front_last = banknumber.substr(0,banknumber.length-1);        var front_arr = new Array();        //将前置部分号码存入数组(前15或18位)        for(var i=front_last.length-1;i>-1;i--) {            //前15或18位倒序存进数组            front_arr.push(front_last.substr(i,1));        }        var sum1 = sum2 = sum3 = 0;        for(var j=0;j<front_arr.length;j++) {            if((j+1)%2==1) {                // 奇数数字和                if(parseInt(front_arr[j])*2<9) {                    sum1 += parseInt(front_arr[j])*2;                } else {                    var str = parseInt(front_arr[j])*2;                    var str1 = 1;                    var str2 = str%10;                    sum2 += str1;                    sum2 += str2;                }            } else {                // 偶数数字和                sum3 += parseInt(front_arr[j]);            }        }        var sum = sum1 + sum2 + sum3;        var luhn = sum%10 == 0?0:10-sum%10;        if(luhn == parseInt(last)){            return true;        }else{            return false;        }    }