/**验证一切*/function checkEverything(type, s) { switch (type.toString().toLocaleLowerCase()) { case "mac1": /**Mac地址正则表达式匹配规则(以:分割,例如 01:00:5e:00:00:02)*/ return /^([0-9a-fA-F]{2})(([/\s:][0-9a-fA-F]{2}){5})$/.test(s); case "mac2": /**Mac地址正则表达式匹配规则(以-分割,例如 01-00-5e-00-00-02)*/ return /^([0-9a-fA-F]{2})(([/\s-][0-9a-fA-F]{2}){5})$/.test(s); case "postcode": /**验证邮政编码*/ return /^(0[1234567]|1[012356]|2[01234567]|3[0123456]|4[01234567]|5[1234567]|6[1234567]|7[012345]|8[013456])\d{4}$/.test(s); case "uppercase": /**验证是否包含大写字母*/ return /[A-Z]/.test(s); case "car": s = s.toString(); if (s.length == 8) { return /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}(([0-9]{5}[DF]$)|([DF][A-HJ-NP-Z0-9][0-9]{4}$))/.test(s); } else if (s.length == 7) { return /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[A-HJ-NP-Z0-9]{4}[A-HJ-NP-Z0-9挂学警港澳]{1}$/.test(s); } else { return false; } case "integer": return /^[0-9]+$/.test(s); case "special": return /[\<\>]/.test(s); case "name":/*中文名*/ return /^[\u4E00-\u9FA5\uf900-\ufa2d·s]{2,20}$/.test(s); case "cn":/*包含中文*/ return /[\u4E00-\u9FA5]/i.test(s); case "id":/*身份证*/ return /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/.test(s); case "user": /*账号*/ return /^[a-zA-z]\w{3,15}$/.test(s); case "httpurl": return /(http|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?/.test(s); case "url": return /^(([A-Za-z0-9-~]+)\.)+([A-Za-z0-9-~\/])+$/.test(s); case "mobile": /*手机号*/ return /^1\d{10}$/.test(s); case "tel": /*座机号*/ return /^0\d{2,3}-?\d{7,8}$/.test(s); case "email": return /^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,6}$/.test(s); case "number": return /^\d+$/g.test(s); case "percentNumber": //匹配 0 - 100 含0,含100,可为两位小数 return /^([0-9]\d{0,1}|100$)(\.\d{1,2})?$/g.test(s); case "percentNumberString": //匹配 0%-100% 含0%,含100%,可为两位小数,且需出现%字符 return /^([0-9]\d{0,1}|100[%]{1}$)(\.\d{1,2})?%?$/g.test(s); case "percentNumberOrString": //匹配 0-100, 0%-100%,可为两位小数,%百分号可有可无 return /^([0-9]\d{0,1}|100[%]{0,1}$)(\.\d{1,2})?%?$/g.test(s); case "password": if (s == "") { return false; } else if (s.split(" ").length != 1) { return false; } else { if ((s.search(/^[a-zA-Z]+$/g) != -1) || (s.search(/^[0-9]+$/g) != -1)) { return false; } else if (s.search(/^[a-zA-Z0-9]+$/g) != -1) { return false; } else { return false; } } return true; } }
/**表单验证神器1.0*/ var vd = { a: function (arr) { /** arr 结构说明: [{ input:"[必填]输入框控件的class or id",(需要特别说明,每个输入框文件都需要加入placeholder) type:"[可选]验证的格式类型(例如:name中文姓名|id身份证|mobile移动手机好(具体去舒工写的checkEverything方法查看))", focus:"[可选]需要聚焦高亮闪烁显示的元素DOM(注意是DOM!!!)(依赖舒工的focusDOM类)", tip:"[可选]当用户输入内容验证失败时候弹出提示框(注意!如果加入了此参数,需要引入layer-ui的离线包,下载地址https://www.layui.com/)", },] */ for (var i = 0, len = arr.length; i < len; i++) { var a = arr[i], input = document.querySelector(a.input), focus = a.focus, type = a.type, tip = a.tip; if (!a.input) return console.log("vd.a方法的arr参数的input属性为空"); var value = input.value.replace(/\ |\ /g, ""); focus || (focus = input); if (!value) { console.log(focus); var txt = "输入不能为空"; input.value = "", input.placeholder = input.placeholder || txt; focusDOM.to(focus, input); tip && layer.msg(txt); return true } if (type && !checkEverything(type, value)) { input.value = "", input.placeholder = tip || input.placeholder; tip && layer.msg(tip); focusDOM.to(focus, input); return true } } return false; } }; /*测试用例*/ document.querySelector("form").onsubmit = function () { if (vd.a([{input: "#a1", type: "name", focus: document.querySelector("#a1").parentNode,}, {input: "#a2", type: "id", focus: document.querySelector("#a2").parentNode, tip: "您输入的身份证格式不正确",},])) return false; return false; };