Bank card verification
Luhn formula is a widely used system to verify the identification number. It doubles the value of every other number according to the original identification number. Then add the values of individual numbers together (if the doubled value is 2 numbers, add the two numbers separately). If the sum can be divided by 10, then the ID number is legal.
replace(/\s/g, ") means to replace a string of a specified number with another string
//Luhn verification algorithm of bank card number
//luhn verification rule: 16 digit bank card number (19 digit universal):
//1. Number the 15 (or 18) digit card number without check digit from right to 15 (18), and multiply the number on odd digit number by 2.
//2. Add all ten bits of odd bit product, and then add all numbers on even bit.
//3. The addition and the check bit can be divided by 10.
//bankno is the bank card number
function luhnCheck(bankno){
var lastNum=bankno.substr(bankno.length-1,1);//Take out the last bit (compare with luhn)
var first15Num=bankno.substr(0,bankno.length-1);//Top 15 or 18
var newArr=new Array();
for(var i=first15Num.length-1;i>-1;i--){ //The first 15 or 18 bits are stored in the array in reverse order
newArr.push(first15Num.substr(i,1));
}
var arrJiShu=new Array(); //Product of odd number * 2 < 9
var arrJiShu2=new Array(); //Product of odd number * 2 > 9
var arrOuShu=new Array(); //Even digit group
for(var j=0;j<newArr.length;j++){
if((j+1)%2==1){//Odd digit
if(parseInt(newArr[j])*2<9)
arrJiShu.push(parseInt(newArr[j])*2);
else
arrJiShu2.push(parseInt(newArr[j])*2);
}
else //Even digit
arrOuShu.push(newArr[j]);
}
var jishu_child1=new Array();//Odd number * 2 > 9
var jishu_child2=new Array();//Odd number * 2 > 9 divided array tens
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; //Sum of arrays with odd number * 2 < 9
var sumOuShu=0; //Sum of even digit groups
var sumJiShuChild1=0; //The sum of the number of odd digits *2 >9
var sumJiShuChild2=0; //Sum of ten digits of the divided array with odd digit * 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]);
}
//Sum of computation
sumTotal=parseInt(sumJiShu)+parseInt(sumOuShu)+parseInt(sumJiShuChild1)+parseInt(sumJiShuChild2);
//Calculate luhn value
var k= parseInt(sumTotal)%10==0?10:parseInt(sumTotal)%10;
var luhn= 10-k;
if(lastNum==luhn){
console.log("Verifying and passing");
return true;
}else{
layer.msg("Bank card number must conform to luhn check");
return false;
}
}
//Using the layui declaration
layui.use('layer', function(){
var layer = layui.layer;
});
//Check bank card number
function CheckBankNo(bankno) {
var bankno = bankno.replace(/\s/g,'');
if(bankno == "") {
layer.msg("Please fill in the bank card number");
return false;
}
if(bankno.length < 16 || bankno.length > 19) {
layer.msg("The bank card number must be between 16 and 19 in length");
return false;
}
var num = /^\d*$/;//all-digital
if(!num.exec(bankno)) {
layer.msg("Bank card number must be all numbers");
return false;
}
//The first 6 bits
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("The first 6 digits of the bank card number do not conform to the specification");
return false;
}
//Luhn check
if(!luhnCheck(bankno)){
return false;
}
return true;
}
Phone number verification
Regular expression: / ^ 1[34578]\d{9}$/ ^1 means: start with 1 [34578] means that any one of the formula brackets can be matched \d{9} means that 9 digits can be matched, and each digit can be any digit from 0 to 9 $means: end
//Verify phone number
function checkPhone(){
var phoneNum = $("#phoneNum").val();
if(phoneNum == "" ){
layer.msg("Mobile number is empty, please fill in!");
return;
}else if(!(/^1[34578]\d{9}$/.test(phoneNum))){
layer.msg("Wrong mobile number, please fill in again!");
}
}
Please pay attention to "zhilaitang learning community", address: http://www.zhiliaotang.com/portal.php