首页 > 代码库 > java工程积累——前台页面的统一校验

java工程积累——前台页面的统一校验

对比:

1,以前的页面验证代码

  <span style="font-size:18px;">//为了验证就医单号是否为空  
        var isDBNull = ‘true‘  
        //判断就医单号是否为空,为空:true,否则false;  
        function isInoputNull(){  
            for(var i = 0;i < detailSourceList.length; i++){  
                  
                var dateStr = $(‘#txtOriginalCode‘ + i)[0].value;  
                  
                if(dateStr.trim()==‘‘){  
                    isDBNull=‘false‘;  
                }  
                      
             }  
    }</span> 

 

这样我们每个需要验证的页面都要在提交时走一遍这个函数,我们如果验证多了,就会在提交表单是写N个类似的函数 ,这样,实际浪费的是我们的时间!

 

2,改进之后

    <span style="font-size:18px;">

<input type="text" vtype="NumAndStr" notNull="true" vdisp="提示信息" id="isOldLeader"   value="http://www.mamicode.com/${LoginUser.NLeader}"/>

    </span> 

在提交表单是,我们只需要调用一个统一的验证入口(doValidate)就可以了

验证js文件:validateform.js

 

  1 /**  2 表单验证类, 可以根据需要,参考下面的代码自行扩充  3 author: guoerhua@126.com  4 使用方法:在标签中可以添加以下属性  5 vtype:   输入域的类型,取值为[LetterStr, NumAndStr, NumStr, ...], 可参考validrule  6 notNull: 是否为空,取值为[true, false]  7 vdisp:   表示提示信息  8 */  9 var validrule                  = new Object(); 10 validrule.LetterStr            = /^([a-zA-Z]+)?$/;      11 validrule.NumAndStr            = /^([0-9a-zA-Z]+)?$/; 12 validrule.NumStr               = /^(\d*)?$/;   13 validrule.string               = /^([^‘<>]+)?$/; 14 validrule.int                  = /^(\d{1,9})?$/;  15 validrule.minusint             = /^(\-([1-9])(\d*))?$/; 16 validrule.float                = /^((\.([0-9]\d*))|(([0-9]\d*)\.\d+$)|([0-9]\d*))?$/;                     17 validrule.date                 = /^((([1-9]\d{3})|([1-9]\d{1}))-(0[1-9]|1[0-2])-(0[1-9]|[1-2]\d|3[0-1]))?$/;   18 validrule.time                 = /^((0[1-9]|1[0-9]|2[0-4]):([0-5][0-9]):([0-5][0-9]))?$/;  19 validrule.datetime             = /^((([1-9]\d{3})|([1-9]\d{1}))-(0[1-9]|1[0-2])-(0[1-9]|[1-2]\d|3[0-1]) (0[1-9]|1[0-9]|2[0-4]):([0-5][0-9]):([0-5][0-9]))?$/;    20 validrule.year                 = /^(\d{4})?$/;  21 validrule.month                = /^([1-9]|0[1-9]|1[0-2])?$/; 22 validrule.day                  = /^([1-9]|0[1-9]|1[0-9]|2[0-9]|3[0-1])?$/; 23 validrule.postcode             = /^(\d{6})?$/;            24 validrule.email                = /^(.+\@.+\..+)?$/;    25 validrule.phone                = /^(\(\d{3}\))?(\(?(\d{3}|\d{4}|\d{5})\)?(-?)(\d+))?((-?)(\d+))?$/;  26 validrule.mobiletel            = /^(013(\d{9})|13(\d{9}))?$/;  27 validrule.ip                   = /^(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-5][0-5])\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-5][0-5])\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-5][0-5])\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-5][0-5]))?$/;   28 validrule.idcard               = /^(\d{15}|\d{18}|\d{17}X|\d{17}x)?$/;  29 validrule.tabledefine          = /^(([A-Za-z])([A-Za-z0-9|_]){1,18})?$/;  30 validrule.integer              = /^(\d{1,9})?$/;  31 validrule.number               = /^(\d*)?$/; 32 validrule.url                  = /^(http:\/\/)/; 33 validrule.chinese              = /^[\u4E00-\u9FA5]*$/; 34 validrule.notsstring           = /^([^‘<>\s]+)?$/; 35 validrule.noEmpty              = /^\s*$/; 36  37 function doValidate( vform )  38 { 39     var elems = vform.elements; 40     var frmLen = elems.length; 41     var thePat = ""; 42     var strFormatInfo = ""; 43     //对于每一个FROM元素 44     for(var i=0;i<frmLen;i++)  45     { 46         var _elem = elems[i]; 47         if(!_elem) 48             continue; 49          50         //为空检查                 51         if(_elem.notNull != null && _elem.notNull == "true")  52         { 53             if(validrule.noEmpty.test(_elem.value))  54             { 55                 alert("请填写"+_elem.vdisp+"!"); 56                 _elem.focus(); 57                 return false; 58             } 59         } 60          61         if(_elem.tagName=="TEXTAREA"){ 62             if(!validateTextareaMaxlength(_elem)){ 63                 return false; 64             } 65         } 66          67         //类型检查                   68         if(_elem.vtype == null)  69         { 70             continue; 71         } 72               if(_elem.vtype=="none") 73               {          74                  thePat = ""; 75                  strFormatInfo = ""; 76               } 77               if(_elem.vtype=="LetterStr") 78               {        79                  thePat = validrule.LetterStr; 80                  strFormatInfo = "纯字母字符串"; 81               } 82               if(_elem.vtype=="NumAndStr") 83               {        84                  thePat = validrule.NumAndStr; 85                  strFormatInfo = "数字和字母字符串"; 86               } 87               if(_elem.vtype=="NumStr") 88               {        89                  thePat = validrule.NumStr; 90                  strFormatInfo = "纯数字组成的字符串"; 91               } 92               if(_elem.vtype=="string") 93               {        94                  thePat = validrule.string; 95                  strFormatInfo = "不能包含特殊符的字符串(英文的‘<>)"; 96               } 97               if(_elem.vtype=="int")          98               {        99                  thePat = validrule.int;100                  strFormatInfo = "整数";101               }102               if(_elem.vtype=="minusint")         103               {       104                  thePat = validrule.minusint;105                  strFormatInfo = "负整数,比如-123";106               }107               if(_elem.vtype=="float")         108               {       109                  thePat = validrule.float;110                  strFormatInfo = "实数,比如356.32";111               }112               if(_elem.vtype=="date")         113                 {       114                  thePat = validrule.date;115                  strFormatInfo = "日期型,比如 2004-08-12";116               }117               if(_elem.vtype=="time")         118               {       119                  thePat = validrule.time;120                  strFormatInfo = "时间型,比如08:37:29";121               }               122               if(_elem.vtype=="datetime")         123               {       124                  thePat = validrule.datetime;125                  strFormatInfo = "日期时间型,比如2004-08-12 08:37:29";126               }127                 if(_elem.vtype=="year")         128               {       129                  thePat = validrule.year;130                  strFormatInfo = "年代格式,比如 2005";131               }132               if(_elem.vtype=="month")         133               {       134                  thePat = validrule.month;135                  strFormatInfo = "月份格式,比如 08";136               }137                 if(_elem.vtype=="day")         138               {       139                  thePat = validrule.day;140                  strFormatInfo = "日子格式,比如 14";141               } 142               if(_elem.vtype=="postcode")         143               {       144                  thePat = validrule.postcode;145                  strFormatInfo = "邮编,比如 100001";146               }              147               if(_elem.vtype=="email")         148               {       149                  thePat = validrule.email;150                  strFormatInfo = "电子邮件格式,比如 msm@hotmail.com";151               }152               if(_elem.vtype=="phone")         153               {       154                  thePat = validrule.phone;155                  strFormatInfo = "电话号码格式,比如010-67891234";156               }157               if(_elem.vtype=="mobiletel")         158               {       159                  thePat = validrule.mobiletel;160                  strFormatInfo = "手机号码格式,比如13867891234";161               }              162               if(_elem.vtype=="ip")       163               {       164                  thePat = validrule.ip;165                  strFormatInfo = "机器ip地址格式,比如 172.22.169.11";166               }167               if(_elem.vtype=="url")       168               {       169                  thePat = validrule.url;170                  strFormatInfo = "url地址格式,比如 http://www.baidu.cn";171               }172               if(_elem.vtype=="idcard")       173               {       174                  thePat = validrule.idcard;175                  strFormatInfo = "身份证号码,比如15位或者18位数字";176               }177               if(_elem.vtype=="tabledefine")   178               {       179                  thePat = validrule.tabledefine;180                  strFormatInfo = "p_tablename";181               }182               if(_elem.vtype=="integer")         183               {       184                  thePat = validrule.integer;185                  strFormatInfo = "整数";186               }187               if(_elem.vtype=="number")         188               {       189                  thePat = validrule.number;190                  strFormatInfo = "数字字符";191               }192               if(_elem.vtype=="chinese")193               {194                  thePat = validrule.chinese;195                  strFormatInfo = "汉字";196               }197               if(_elem.vtype == "notsstring")198               {199                  thePat = validrule.notsstring;200                  strFormatInfo = "不包含空格和特殊字符的字符串(英文的‘<>)";201               }202                             203               var gotIt = null; 204               if(thePat!="")205               {206                     gotIt = thePat.exec(_elem.value);207               }               208               if(gotIt == null) 209               {210                   alert(_elem.vdisp+"输入不合法,格式应为:"+strFormatInfo);211                   _elem.focus();212                   return false;213               }214     }  215     return true;216 }217 218 function validateTextareaMaxlength(_elem){219     var strLen = stringLength(_elem.value);220     if(_elem.maxlength != null  &&  strLen > _elem.maxlength) 221     {222         var vdisp = _elem.vdisp || "";223         alert(vdisp + "超过限定长度,请检查! \r\n\r\n" + vdisp + "限定长度为" + _elem.maxlength + ", 实际长度为" + strLen + ". \r\n(中文字符长度为2)");224         _elem.focus();225         return false;226     }227     return true;228 }229 230 function stringLength(str){231     if(str==null) return 0;232     var n = 0;233     for(var i=0; i<str.length; i++){234         if(str.charCodeAt(i)>255)235             n = n + 2;236         else n++;237     }238     return n;239 }
View Code

 

java工程积累——前台页面的统一校验