JS校驗銀行卡號、輸入卡號時放大效果
阿新 • • 發佈:2017-06-04
比較 left for 取出 abs focusout htm length rep 一、(校驗格式)
function CheckBankNo(t_bankno) {
var bankno = $.trim(t_bankno);
if(bankno == "") {
layer.msg("請填寫銀行卡號");
return false;
}
if(bankno.length < 16 || bankno.length > 19) {
layer.msg("銀行卡號長度必須在16到19之間");
return false;
}
var num = /^\d*$/; //全數字
if(!num.exec(bankno)) {
layer.msg("銀行卡號必須全為數字");
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) {
layer.msg("銀行卡號開頭6位不符合規範");
return false;
}
//Luhm校驗(新)
var lastNum=bankno.substr(bankno.length-1,1);//取出最後一位(與luhm進行比較)
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<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{
layer.msg("銀行卡號不符合規範");
return false;
}
}
二、(輸入時放大)
//1、CSS:放大的樣式
<style>
.inputCard-wrap {
position: relative;
}
.inputCard-wrap .panelCard {
display: none;
position: absolute;
top: -30px;
left: 79px;
z-index: 100;
border: 1px #ffce6e solid;
padding: 10px;
height: 8px;
font-size: 1.7em;
line-height: 8px;
color: #585858;
}
</style>
//2、內容:
<div class="divfirst inputCard-wrap">
銀行卡號:
<input type="text" name="bankCardNumber" class="input-text size-S inputCard"
id="bankCardNumber"
style="margin-left: 5px;" placeholder="輸入銀行卡號">
<div class="panelCard"></div>
</div>
//3、JS:
<script>
$(function () {
/* 銀行卡號實時驗證放大顯示 */
$(".inputCard").keyup(function (e) {
var self = $.trim(e.target.value),
parent = $(e.target).closest(".inputCard-wrap"),
panel = $(".panelCard", parent),
val = self.replace(/\D/g, ‘‘);
if (self.length > 19) {
this.value = self.substr(0, 19);
return this.value;
}
if (val == self) {
panel.show();
val = self.replace(/(....)(?=.)/g, ‘$1 ‘);
panel.html(val);
} else {
panel.hide();
return self;
}
});
$(".inputCard").unbind(‘focusin‘);
$(".inputCard").bind(‘focusin‘, function (e) {
var self = $.trim(e.target.value),
parent = $(e.target).closest(".inputCard-wrap"),
panel = $(".panelCard", parent),
val = self.replace(/(....)(?=.)/g, ‘$1 ‘);
if (val != ‘‘) {
panel.show();
panel.html(val);
}
});
$(".inputCard").unbind(‘focusout‘);
$(".inputCard").bind(‘focusout‘, function (e) {
var parent = $(e.target).closest(".inputCard-wrap"),
panel = $(".panelCard", parent);
panel.hide();
});
})
</script>
JS校驗銀行卡號、輸入卡號時放大效果