JavaScript中校验银行卡号的实现代码

1、先引入jquery.js

2、接着引入luhmCheck.js  //银行卡号Luhm校验

3、看下面的案例:

<!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>
    <title>无标题页</title>
    <script type="text/javascript" src="js/jquery.js"></script>
    <script type="text/javascript" src="js/luhmCheck.js"></script>
  </head>
  <body>
    <input type="text" id="t_bankno" />
    <input type="button" id="btnCheck" value="check" onclick="CheckBankNo($('#t_bankno'));" />
    <span id="banknoInfo"></span>
  </body>
</html>

下面是js

<script type="text/javascript">
  function CheckBankNo(t_bankno) {
    var bankno = $.trim(t_bankno.val());
    if(bankno == "") {
      $("#banknoInfo").html("请填写银行卡号");
     return false;
   }
   if(bankno.length < 16 || bankno.length > 19) {
     $("#banknoInfo").html("银行卡号长度必须在16到19之间");
     return false;
   }
   var num = /^\d*$/; //全数字
   if(!num.exec(bankno)) {
     $("#banknoInfo").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(bankno.substring(0, 2)) == -1) {
     $("#banknoInfo").html("银行卡号开头6位不符合规范");
     return false;
   }
   //Luhm校验(新)
   if(!luhmCheck(bankno))
     return false;
     $("#banknoInfo").html("验证通过!");
     return true;
   }
</script>

测试卡号:

1、6222600810010710887

2、6225881414207430

相关推荐