js银⾏卡luhm校验,验证银⾏卡号并返回开户⾏(三⽅插件和
在线验证)
最近做公司的项⽬遇到银⾏卡号匹配开户银⾏的问题,⼀开始是正则匹配银⾏卡号码,开户银⾏银⾏⾃⼰填写,后来⽤户⼜让改,说要⾃动匹配开户银⾏,唉,⽤户都是⼤哥,让改就要改……话不多说,上代码。
⾃⼰寻思了半天,上⽹看了⼀些⼤神的思路,最后做出来,开户银⾏系统匹配,银⾏卡号码使⽤正则来验证,以下添加两种验证⽅法,在线验证是后来补充的。
⽅法⼀:三⽅插件,可以匹配银⾏卡号和开户银⾏
form表单⾥的引⽤,因为我这⾥是使⽤ajax提交的,所以展⽰给⽤户的使⽤的p标签,实际的值是表单隐藏域:
<!-- form 表单 -->
<div class="row cl">
<label class="form-label col-xs-4 col-sm-2">开户银⾏</label>
<div class="formControls col-xs-8 col-sm-9">
<input type="hidden" name="b_name">
<p id="b_name" >输⼊卡号⾃⾏匹配</p>
</div>
<div class="col-xs-8 col-sm-6 col-xs-offset-4 col-sm-offset-2"> </div>
</div>
//依赖于js库,不要忘记引⽤哦,这⾥我就没有提供js库啦
<script src="__STYLE__/js/luhmCheck.js"></script>
<script src="__STYLE__/js/bankcard.js"></script>
<script>
$(function(){
/
/银⾏卡验证
$("input[name='b_card']").blur(function(){
if($("input[name='b_card']").val() != ""){
if($(".b_card").length < 15 || $(".b_card").length > 20) {
alert("银⾏卡号长度必须在16到19之间!");
return false;
}
var num = /^\d*$/; //全数字
if(!st($(".b_card").val())) {
alert("银⾏卡号必须全为数字!");
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($(".b_card").val().substring(0,2)) == -1) {
alert("银⾏卡号开头6位不符合规范!");
return false;
}
//Luhm校验(新) luhmCheck()是验证银⾏卡号码正误的
if(!luhmCheck($(".b_card").val())){
alert("银⾏卡号码有误!");
return false;
}else{
//这⾥是展⽰给⽤户看得银⾏卡开户名以及传递给表单隐藏域的bankCardAttribution()⽅法就是⾃动匹配开户银⾏的
var b_name = bankCardAttribution($(".b_card").val());
js获取json的key和value$("input[name='b_name']").val(b_name['bankName']);
$("#b_name").text(b_name['bankName']);
}
}else{
alert("请输⼊银⾏卡号码!");
return false;
}
});
});
</script>
关于银⾏卡开户银⾏的匹配,js封装的字段在⾕歌浏览器中使⽤(console.log)打印如下:
⽅法⼆:在线调⽤⽀付宝验证接⼝验证,这种⽅法可以避免银⾏新增的时候我们本地插件不完整这种情况发⽣
<input type="text" name="bank" id="bank" autocomplete="off" class="layui-input">
//银⾏卡验证
$("#bank").blur(function(){
var value = $(this).val(); //获取当前输⼊框的值
$.post("ccdcapi.alipay/validateAndCacheCardInfo.json",{cardNo:value,cardBinCheck:'true'},function(res){ //console.log(res); //不清楚返回值的打印出来看
//{"cardType":"DC","bank":"ICBC","key":"622200****412565805","messages":[],"validated":true,"stat":"ok"}
if(res.validated){
layer.msg('银⾏卡号正确',{icon:6});
return true;
}else{
layer.msg('银⾏卡号错误',{icon:5});
setTimeout($("#bank").focus(),1); //获取焦点
return false;
}
},'json');
});
//银⾏及其简称,注意数组式json格式哈,取值⽅法略需修改
var bankList = {PSBC: "中国邮政储蓄银⾏", ICBC: "中国⼯商银⾏", ABC: "中国农业银⾏", BOC: "中国银⾏",
CCB: "中国建设银⾏",
COMM: "中国交通银⾏",
CMB: "招商银⾏",
CMBC: "中国民⽣银⾏",
CEB: "中国光⼤银⾏",
CITIC: "中信银⾏",
HXBANK: "华夏银⾏",
SPABANK: "深发/平安银⾏",
CIB: "兴业银⾏",
SHBANK: "上海银⾏",
SPDB: "浦东发展银⾏",
GDB: "⼴发银⾏",
BOHAIB: "渤海银⾏",
GCB: "⼴州银⾏",
JHBANK: "⾦华银⾏",
WZCB: "温州银⾏",
HSBANK: "徽商银⾏",
SCB: "渣打银⾏",
CITI: "花旗银⾏",
HKBEA: "东亚银⾏",
GHB: "⼴东华兴银⾏",
SRCB: "深圳农村商业银⾏",
GZRCU: "⼴州农村商业银⾏股份有限公司",
DRCBCL: "东莞农村商业银⾏",
BOD: "东莞市商业银⾏",
GDRCC: "⼴东省农村信⽤社联合社",
DSB: "⼤新银⾏",
WHB: "永亨银⾏",
DBS: "星展银⾏⾹港有限公司",
EGBANK: "恒丰银⾏",
TCCB: "天津市商业银⾏",
CZBANK: "浙商银⾏",
NCB: "南洋商业银⾏",
XMBANK: "厦门银⾏",
FJHXBC: "福建海峡银⾏",
JLBANK: "吉林银⾏",
HKB: "汉⼝银⾏",
SJBANK: "盛京银⾏",
DLB: "⼤连银⾏",
BHB: "河北银⾏",
URMQCCB: "乌鲁⽊齐市商业银⾏",
SXCB: "绍兴银⾏",
CDCB: "成都商业银⾏",
FSCB: "抚顺银⾏",
ZZBANK: "郑州银⾏",
NXBANK: "宁夏银⾏",
CQBANK: "重庆银⾏",
HRBANK: "哈尔滨银⾏",
LZYH: "兰州银⾏",
QDCCB: "青岛银⾏",
QHDCCB: "秦皇岛市商业银⾏",
BOQH: "青海银⾏",
TZCB: "台州银⾏",
CSCB: "长沙银⾏",
BOQZ: "泉州银⾏",
BSB: "包商银⾏",
DAQINGB: "龙江银⾏",
SHRCB: "上海农商银⾏",
ZJQL: "浙江泰隆商业银⾏",
H3CB: "内蒙古银⾏",
BGB: "⼴西北部湾银⾏",
GLBANK: "桂林银⾏",
DAQINGB: "龙江银⾏",
CDRCB: "成都农村商业银⾏",
FJNX: "福建省农村信⽤社联合社",
TRCB: "天津农村商业银⾏",
JSRCU: "江苏省农村信⽤社联合社",
SLH: "湖南农村信⽤社联合社",
JXNCX: "江西省农村信⽤社联合社",
SCBBANK: "商丘市商业银⾏",
HRXJB: "华融湘江银⾏",
HSBK: "衡⽔市商业银⾏",
CQNCSYCZ: "重庆南川⽯银村镇银⾏",
HNRCC: "湖南省农村信⽤社联合社",
XTB: "邢台银⾏",
LPRDNCXYS: "临汾市尧都区农村信⽤合作联社",
WHSYBANK: "威海市商业银⾏",
HZBANK: "湖州银⾏",
BANKWF: "潍坊银⾏",
GZB: "赣州银⾏",
RZGWYBANK: "⽇照银⾏",
NCB: "南昌银⾏",
GYCB: "贵阳银⾏",
BOJZ: "锦州银⾏",
QSBANK: "齐商银⾏",
RBOZ: "珠海华润银⾏",
HLDCCB: "葫芦岛市商业银⾏",
HBC: "宜昌市商业银⾏",
HZCB: "杭州商业银⾏",
JSBANK: "苏州市商业银⾏",
LYCB: "辽阳银⾏",
LYB: "洛阳银⾏",
JZCBANK: "焦作市商业银⾏",
ZJCCB: "镇江市商业银⾏",
FGXYBANK: "法国兴业银⾏",
DYBANK: "⼤华银⾏",
DIYEBANK: "企业银⾏",
HQBANK: "华侨银⾏",
HSB: "恒⽣银⾏",
LSB: "临沂商业银⾏",
YTCB: "烟台商业银⾏",
QLB: "齐鲁银⾏",
BCCC: "BC卡公司",
CYB: "集友银⾏",
TFB: "⼤丰银⾏",
AEON: "AEON信贷财务亚洲有限公司", MABDA: "澳门BDA"
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论