常用表单校验(手机号、固话、身份证、真是姓名、邮箱、银行卡)
1.js校验手机号
// 手机号码正则表达式 var mobile = /^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\d{8}$/; //19位全国正式唯一学籍号正则表达式 var roll_code = /^[A-Z]\d{6}(18|19|20)?\d{2}(0[1-9]|1[012])(0[1-9]|[12]\d|3[01])\d{3}(\d|[xX])$/; //18位身份证正则表达式 var identity_card = /^\d{6}(18|19|20)?\d{2}(0[1-9]|1[012])(0[1-9]|[12]\d|3[01])\d{3}(\d|[xX])$/; //保留一位小数的正数正则表达式 var reg = /(^[1-9](\d+)?(\.\d{1,1})?$)|(^(0){1}$)|(^\d\.\d{1,1}?$)/; //onkeydown事件禁用文本框粘帖 function fncKeyStop(evt){ if(!window.event){ var keycode = evt.keyCode; var key = String.fromCharCode(keycode).toLowerCase(); if(evt.ctrlKey && key == "v"){ evt.preventDefault(); evt.stopPropagation(); } } }
2.js验证身份证号
js验证身份证号,中国的身份证号,一代身份证号是15位的数字,二代身份证都是18位的,最后一位校验位除了可能是数字还可能是'X'或'x',所以有四种可能性:a.15位数字 b.18位数字 c.17位数字,第十八位是'X' d.17位数字,第十八位是'x'
// 身份证正则校验
var regIdNo = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/; if(!regIdNo.test(idNo)){ alert('身份证号填写有误'); return false; }
//身份证号合法性验证 //支持15位和18位身份证号 //支持地址编码、出生日期、校验位验证 const IdentityCodeValid = code => { var city = { 11: "北京", 12: "天津", 13: "河北", 14: "山西", 15: "内蒙古", 21: "辽宁", 22: "吉林", 23: "黑龙江 ", 31: "上海", 32: "江苏", 33: "浙江", 34: "安徽", 35: "福建", 36: "江西", 37: "山东", 41: "河南", 42: "湖北 ", 43: "湖南", 44: "广东", 45: "广西", 46: "海南", 50: "重庆", 51: "四川", 52: "贵州", 53: "云南", 54: "西藏 ", 61: "陕西", 62: "甘肃", 63: "青海", 64: "宁夏", 65: "新疆", 71: "台湾", 81: "香港", 82: "澳门", 91: "国外 " }; var tip = ""; var pass = true; //验证身份证格式(6个地区编码,8位出生日期,3位顺序号,1位校验位) if (!code || !/^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$|^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/.test(code)) { tip = "身份证号格式错误"; pass = false; } else if (!city[code.substr(0, 2)]) { tip = "地址编码错误"; pass = false; } else { //18位身份证需要验证最后一位校验位 if (code.length == 18) { code = code.split(''); //∑(ai×Wi)(mod 11) //加权因子 var factor = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]; //校验位 var parity = [1, 0, 'X', 9, 8, 7, 6, 5, 4, 3, 2]; var sum = 0; var ai = 0; var wi = 0; for (var i = 0; i < 17; i++) { ai = code[i]; wi = factor[i]; sum += ai * wi; } var last = parity[sum % 11]; if (parity[sum % 11] != code[17]) { tip = "校验位错误"; pass = false; } } } return pass; }
3.js验证真实姓名
js验证真实姓名,是用的unicode字符的来进行匹配,而中国人的姓名长度一般都是2-4,所以重复匹配{2,4}次
var regName =/^[\u4e00-\u9fa5]{2,4}$/; if(!regName.test(name)){ alert('真实姓名填写有误'); return false; }
4.正则校验只能输入汉字数字英文下划线
let REG = /^[\u4e00-\u9fa5\w]{1,6}$/; console.log(REG.test('你的用户名')); // true 或者 false
5.校验邮箱正则
/^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/
6.固话校验
/^((0\d{2,3}-\d{7,8})|(1[3584]\d{9}))$/
7.银行卡校验
/** *银行卡号Luhn校验算法 *luhn校验规则:16位银行卡号(19位通用): *1.将未带校验位的 15(或18)位卡号从右依次编号 1 到 15(18),位于奇数位号上的数字乘以 2。 *2.将奇位乘积的个十位全部相加,再加上所有偶数位上的数字。 *3.将加法和加上校验位能被 10 整除。 *4.bankno为银行卡号 **/ const luhnCheck = bankno => { let lastNum = bankno.substr(bankno.length - 1, 1); //取出最后一位(与luhn进行比较) let first15Num = bankno.substr(0, bankno.length - 1); //前15或18位 let newArr = new Array(); for (let i = first15Num.length - 1; i > -1; i--) { //前15或18位倒序存进数组 newArr.push(first15Num.substr(i, 1)); } let arrJiShu = new Array(); //奇数位*2的积 <9 let arrJiShu2 = new Array(); //奇数位*2的积 >9 let arrOuShu = new Array(); //偶数位数组 for (let 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]); } let jishu_child1 = new Array(); //奇数位*2 >9 的分割之后的数组个位数 let jishu_child2 = new Array(); //奇数位*2 >9 的分割之后的数组十位数 for (let h = 0; h < arrJiShu2.length; h++) { jishu_child1.push(parseInt(arrJiShu2[h]) % 10); jishu_child2.push(parseInt(arrJiShu2[h]) / 10); } let sumJiShu = 0; //奇数位*2 < 9 的数组之和 let sumOuShu = 0; //偶数位数组之和 let sumJiShuChild1 = 0; //奇数位*2 >9 的分割之后的数组个位数之和 let sumJiShuChild2 = 0; //奇数位*2 >9 的分割之后的数组十位数之和 let sumTotal = 0; for (let m = 0; m < arrJiShu.length; m++) { sumJiShu = sumJiShu + parseInt(arrJiShu[m]); } for (let n = 0; n < arrOuShu.length; n++) { sumOuShu = sumOuShu + parseInt(arrOuShu[n]); } for (let 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); //计算luhn值 let k = parseInt(sumTotal) % 10 == 0 ? 10 : parseInt(sumTotal) % 10; let luhn = 10 - k; if (lastNum == luhn) { console.log("验证通过"); return true; } else { console.log("银行卡号必须符合luhn校验"); return false; } } //检查银行卡号 const CheckBankNo = _bankno => { let bankno = _bankno.replace(/\s/g, ''); if (bankno == "") { console.log("请填写银行卡号"); return false; } if (bankno.length < 16 || bankno.length > 19) { console.log("银行卡号长度必须在16到19之间"); return false; } let num = /^\d*$/; //全数字 if (!num.exec(bankno)) { console.log("银行卡号必须全为数字"); return false; } //开头6位 let 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) { console.log("银行卡号开头6位不符合规范"); return false; } //Luhn校验 if (!luhnCheck(bankno)) { return false; } return true; }
8.判断输入内容是否包含表情
// 判断是否包含表情 const isEmojiCharacter = (substring) => { for (var i = 0; i < substring.length; i++) { var hs = substring.charCodeAt(i); if (0xd800 <= hs && hs <= 0xdbff) { if (substring.length > 1) { var ls = substring.charCodeAt(i + 1); var uc = ((hs - 0xd800) * 0x400) + (ls - 0xdc00) + 0x10000; if (0x1d000 <= uc && uc <= 0x1f77f) { return true; } } } else if (substring.length > 1) { var ls = substring.charCodeAt(i + 1); if (ls == 0x20e3) { return true; } } else { if (0x2100 <= hs && hs <= 0x27ff) { return true; } else if (0x2B05 <= hs && hs <= 0x2b07) { return true; } else if (0x2934 <= hs && hs <= 0x2935) { return true; } else if (0x3297 <= hs && hs <= 0x3299) { return true; } else if (hs == 0xa9 || hs == 0xae || hs == 0x303d || hs == 0x3030 || hs == 0x2b55 || hs == 0x2b1c || hs == 0x2b1b || hs == 0x2b50) { return true; } } } }