侧边栏壁纸
博主昵称
404

  • 累计撰写 17 篇文章
  • 累计收到 0 条评论
JS

luhn验证

404
404
2022-07-02 / 0 评论 / 28 阅读 / 正在检测是否收录...
<script type="text/javascript">
function luhn(bankno){
         alert(bankno);
        var lastNum = bankno.substr(bankno.length-1,1);
        var first15Num = bankno.substr(0,bankno.length-1);
        var newArr = new Array();
        for(var i = first15Num.length-1;i>-1;i--){  
            newArr.push(first15Num.substr(i,1));
        }
        var arrJiShu = new Array(); 
        var arrJiShu2 = new Array();  
        var arrOuShu = new Array(); 
        for(var j=0;j<newArr.length;j++){
            if((j+1)%2==1){
                if(parseInt(newArr[j])*2<9)
                arrJiShu.push(parseInt(newArr[j])*2);
                else
                arrJiShu2.push(parseInt(newArr[j])*2);
            }
            else 
            arrOuShu.push(newArr[j]);
        }
        var jishu_child1 = new Array();
        var jishu_child2 = new Array();
        for(var h=0;h<arrJiShu2.length;h++){
            jishu_child1.push(parseInt(arrJiShu2[h])%10);
            jishu_child2.push(parseInt(arrJiShu2[h])/10);
        }        
        
        var sumJiShu=0; 
        var sumOuShu=0; 
        var sumJiShuChild1=0; 
        var sumJiShuChild2=0; 
        var sumTotal=0;
        for(var m=0;m<arrJiShu.length;m++){
            sumJiShu=sumJiShu+parseInt(arrJiShu[m]);
        }
        
        for(var n=0;n<arrOuShu.length;n++){
            sumOuShu=sumOuShu+parseInt(arrOuShu[n]);
        }
        
        for(var p=0;p<jishu_child1.length;p++){
            sumJiShuChild1=sumJiShuChild1+parseInt(jishu_child1[p]);
            sumJiShuChild2=sumJiShuChild2+parseInt(jishu_child2[p]);
        }      
        sumTotal=parseInt(sumJiShu)+parseInt(sumOuShu)+parseInt(sumJiShuChild1)+parseInt(sumJiShuChild2);
        var k= parseInt(sumTotal)%10==0?10:parseInt(sumTotal)%10;        
        var luhn= 10-k;

        if(lastNum==luhn){
            $("#banknoInfo").html("luhn验证通过");
            return true;
        }
        else{
            $("#banknoInfo").html("银行卡号必须符合luhn校验");
            return false;
        }        
    }
var s = '4297690141496200'
 alert(luhn(s));
</script>
0

评论 (0)

取消