银行卡号
//验证银行卡号,bankno为银行卡号
function luhnCheck(){
var bankno = $.trim($("#bankNoInp").val());
var lastNum=bankno.substr(bankno.length-1,1);//取出最后一位(与luhn进行比较)
var first15Num=bankno.substr(0,bankno.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 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 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 sumJiShu=sumJiShu+parseInt(arrJiShu[m]);
}
for(var n=0;n sumOuShu=sumOuShu+parseInt(arrOuShu[n]);
}
for(var p=0;p sumJiShuChild1=sumJiShuChild1+parseInt(jishu_child1[p]);
sumJiShuChild2=sumJiShuChild2+parseInt(jishu_child2[p]);
}
//计算总和
sumTotal=parseInt(sumJiShu)+parseInt(sumOuShu)+parseInt(sumJiShuChild1)+parseInt(sumJiShuChild2);
//计算luhn值
var k= parseInt(sumTotal)%10==0?10:parseInt(sumTotal)%10;
var luhn= 10-k;
if(lastNum==luhn){
console.log("验证通过");
return true;
}else{
alert("银行卡号必须符合luhn校验");
return false;
}
}
银行卡号位数限制
regs = /^(\d{16}|\d{19})$/;
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";
校验银行卡号案例
//新增银行卡
function addBank(){
var bankNo = $.trim($("#bankNoInp").val());
var bankName = $.trim($("#bankNameInp").val());
var bankAttr = $.trim($("#bankAttrInp").val());
reg = /^[\u4E00-\u9FA5]{2,10}$/;
regs = /^(\d{16}|\d{19})$/;
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(bankNo.length <= 0 ){
alert("银行卡号不能为空!");
}else{
// 银行卡号不为空时,判断银行卡号是否正确
if( !regs.test(bankNo) || strBin.indexOf(bankNo.substring(0, 2)) == -1 || !luhnCheck(bankNo) ){
alert("银行卡号输入错误!");
}else{
// 银行卡号输入正确时,判断开户名,开户银行是否为空
if(bankName == "" || bankAttr == ""){
alert("开户名与开户银行不能为空!");
}else{
// 开户行不为空时,判断开户行是否合法
if(!reg.test(bankName) || !reg.test(bankAttr)){
alert("请输入合法开户名与开户银行!");
}else{
var str = "1";
}
}
}
if(str == "1" ){
$.ajax({
type:'post',
url:'../BankController/insertBank.action',
dataType:'Json',//服务器返回的数据类型 可选XML ,Json jsonp script html text等
data:{
"bankNo": bankNo,
"bankName": bankName,
"bankAttribute": bankAttr
},
success:function(msg){
if(msg.success){
alert("添加帐号成功!");
getData();
hideDialog();
}else{
alert(msg.error);
}
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
sessionTimeout(XMLHttpRequest, textStatus, errorThrown);
}
})
}
}
}
只允许输入汉字以及汉字输入位数
//2-10位
reg = /^[\u4E00-\u9FA5]{2,10}$/;