Home >Web Front-end >JS Tutorial >How to verify bank card account code sharing with js and jq

How to verify bank card account code sharing with js and jq

小云云
小云云Original
2018-03-06 16:10:512723browse

This article mainly shares with you how to verify the bank card account code with js and jq. This article mainly shares it through the code. I hope it can help everyone.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf" />
<script type="text/javascript" src="js/jquery.1.8.3.min.js"></script>
<title>验证银行账号</title>
<style type="text/css">
<!--
.STYLE1 {color: #FF0000}
-->
</style>
<script language="javascript" type="text/javascript">
function check(){
var account = document.form1.account.value;
if (account.length < 16 || account.length > 19) {
alert("银行卡号长度必须在16到19之间");
//$("#accountInfo").html("银行卡号长度必须在16到19之间");
return false;
}
var num = /^\d*$/;  //全数字
if (!num.exec(account)) {
alert("银行卡号必须全为数字");
//$("#accountInfo").html("银行卡号必须全为数字");
return false;
}
//开头6位
var strBin="10,18,30,35,37,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,58,60,62,65,68,69,84,87,88,94,95,98,99";    
if (strBin.indexOf(account.substring(0, 2))== -1) {
alert("银行卡号开头6位不符合规范");
//$("#accountInfo").html("银行卡号开头6位不符合规范");
return false;
}
    var lastNum=account.substr(account.length-1,1);//取出最后一位(与luhm进行比较)
    var first15Num=account.substr(0,account.length-1);//前15或18位
    var newArr=new Array();
    for(var i=first15Num.length-1;i>-1;i--){    //前15或18位倒序存进数组
        newArr.push(first15Num.substr(i,1));
    }
    var arrJiShu=new Array();  //奇数位*2的积 <9
    var arrJiShu2=new Array(); //奇数位*2的积 >9
    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();//奇数位*2 >9 的分割之后的数组个位数
    var jishu_child2=new Array();//奇数位*2 >9 的分割之后的数组十位数
    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; //奇数位*2 < 9 的数组之和
    var sumOuShu=0; //偶数位数组之和
    var sumJiShuChild1=0; //奇数位*2 >9 的分割之后的数组个位数之和
    var sumJiShuChild2=0; //奇数位*2 >9 的分割之后的数组十位数之和
    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);
    //计算Luhm值
    var k= parseInt(sumTotal)%10==0?10:parseInt(sumTotal)%10;        
    var luhm= 10-k;
    if(lastNum==luhm){
    return true;
    }
    else{
    return false;
    }        
}
</script>
</head>
<body>
<form id="form1" name="form1" method="post" action="">
<p>账号:
    <input type="text" name="account" id="account" />
  <span class="STYLE1">* (19位数字)</span></p>
<p>    
    <input type="button" name="button" id="button" value="验证" onclick="check()" />
</p>
<!--<p id="accountInfo"></p>-->
</form>
</body>
</html>

Related recommendations:

JS uses regular rules to control user input of bank card numbers and formatting

Based on jquery implementation of bank card number intervals 4-digit implementation code for automatically inserting spaces

Bank card real-name authentication interface calling code PHP example

The above is the detailed content of How to verify bank card account code sharing with js and jq. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn