/** * @info 前端input验证类 * @version 20200213 * @author chenzhi * var obj = { "type": ["required", "点击事件不能为空",'select'], "sort":["number", "排序不能为空"], "click_option": ["nocheck", "配置信息", "textarea"], "remark": ["nocheck", "备注无需验证", "textarea"], } */ (function(){ function czinputcheck(){ var cic = this; cic.verify= { required: [/[\S]+/, "必填项不能为空"], phone: [/^1\d{10}$/, "请输入正确的手机号"], email: [/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/, "邮箱格式不正确"], url: [/(^#)|(^http(s*):\/\/[^\s]+\.[^\s]+)/, "链接格式不正确"], intnumber: [/^\d+$/, "数字格式错误,数字不能含有小数"], number:[/^\d+(\.{1}\d+)?$/,"数字格式错误"], date: [/^(\d{4})[-\/](\d{1}|0\d{1}|1[0-2])([-\/](\d{1}|0\d{1}|[1-2][0-9]|3[0-1]))*$/, "日期格式不正确"], identity: [/(^\d{15}$)|(^\d{17}(x|X|\d)$)/, "请输入正确的身份证号"], password:[/^[\S]{6,12}$/,"密码必须6到12位,且不能出现空格"], isbn:[/^\d{13}$/, "isbn格式错误"], price:[/(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/,"价格格式错误"], bankid:[/^([1-9]{1})\d{9,19}$/,"银行卡格式错误"], nocheck:[false,"无需验证,存在填,不存在舍弃"], realname:[/[\u4e00-\u9fa5]/,"名字只能是中文"], checkno:[false,"验证,存在填,不存在报错"], }; cic.checkAddInput=function(arr){ var rearr = {}; for(var i in arr) { // var reg = cic.verify[arr[i][0]][0]; var name = "input"; var selecttypename=''; if(typeof arr[i][2] !== "undefined") name = arr[i][2]; if(name == "radio") { name="input"; selecttypename=":checked"; } var reg = arr[i][0]; if(!Array.isArray(reg) || reg.length == 1){ reg = [arr[i][0],false];//默认false } var val = $(name+"[name='"+i+"']"+selecttypename).val(); //为true不存在不报错 if(reg[1]){ if(val == ''){ rearr[i]=val; continue; } } var treg = cic.verify[reg[0]][0]; if(!treg){ if(val != ''){ rearr[i]=val; } }else{ if(treg.test(val)) { rearr[i] =val; }else { layer.alert(arr[i][1], { icon: 2, skin: 'layer-ext-moon' }); return false; continue; } } } return rearr; }; /** * * var obj = { "type": ["required",fale,"点击事件不能为空",'select'], //1 true:为空不验证 false 为空报错 } */ cic.checkInput=function(arr){ for(var i in arr) { var name = "input"; var selecttypename=''; if(typeof arr[i][3] !== "undefined") name = arr[i][3]; if(name == "radio") { name="input"; selecttypename=":checked"; } var reg = cic.verify[arr[i][0]][0]; var val = $(name+"[name='"+i+"']"+selecttypename).val(); if(arr[i][1] && val == ''){ continue; } if(!reg.test(val)) { layer.alert(arr[i][2], { icon: 2, skin: 'layer-ext-moon' }); return false; } } return true; }; cic.setEditData=function(ydata,ndata){ var data = {}; for (var i in ndata) { if(ndata[i] != ydata[i]){ data[i] = ndata[i] } } if($.isEmptyObject(data)) { parent.layer.close(parent.layer.getFrameIndex(window.name)); return false; }else{ data.id = ydata.id; return data; } }; }; window.CIC = new czinputcheck(); })()