日期:2014-05-16  浏览次数:20379 次

优化js,我这个js验证太长了,太大了谁有办法验证时候简略点?
JScript code

    function invalidate()
            {
               //日期不能为空
               document.getElementById("lblFcRefuelDateTime").innerHTML="";
               if (document.getElementById("txtFcRefuelDateTime").value=='')
               {    document.getElementById("lblFcRefuelDateTime").innerHTML="日期不能为空";
                    document.getElementById("txtFcRefuelDateTime").focus();
                    return  false  ;  }
              
               //txt只读 没什么用了
               if (   result =/^\d{4}-\d{2}-\d{2}$/.test(document.getElementById("txtFcRefuelDateTime")))
               {
                    document.getElementById("lblFcRefuelDateTime").innerHTML="请填写正确的加油日期";
                    document.getElementById("txtFcRefuelDateTime").focus();
                    return  false  ;
               }
                 //不能大于今天的日期
                 var textTime=  document.getElementById("txtFcRefuelDateTime").value;
                 var nowDate = new Date();
                 var nowTime=  nowDate.format("yyyy-MM-dd");
               if ( textTime > nowTime)
               {
                    document.getElementById("lblFcRefuelDateTime").innerHTML="日期不能大于今天";
                    document.getElementById("txtFcRefuelDateTime").focus();
                    return  false  ;
               }
                document.getElementById("lblFcRefuelDateTime").innerHTML="";                             
               //不能小于 上一条记录日期
                 var  backTime =  document.getElementById("hdnOldFcTime").value;
                 backTime=  backTime.toString("yyyy-MM-dd");
               //alert(backTime); 
               if( backTime > textTime )
               {    document.getElementById("lblFcRefuelDateTime").innerHTML="时间不能小于上一条记录";
                    document.getElementById("txtFcRefuelDateTime").focus();
                    return  false  ;
               }
               document.getElementById("lblFcRefuelDateTime").innerHTML="";
               // 请填写燃油类型
                if (document.getElementById("sltFcFuelType").selectedIndex==0)
               {   document.getElementById("lblFcFuelType").innerHTML="请选择燃油类型";
                   document.getElementById("sltFcFuelType").focus();
                   return  false  ;
               }
               //请填写加油数
                if (document.getElementById("txtFcRefuel").value=='')
               {    document.getElementById("lblFcRefuel").innerHTML="请填写加油数";
                    document.getElementById("txtFcRefuel").focus();
                    return  false  ;
               }
               //验证小数
               var str=document.getElementById("txtFcRefuel").value;
               var pattern =/^[0-9]+([.]\d{1,2})?$/;
               if(!pattern.test(str)){
                     document.getElementById("lblFcRefuel").innerHTML="请输入数字(例:0.00),最高保留两位小数";      
                     document.getElementById("txtFcRefuel").focus();
                     return false;
                }
               //加油数要大于0
                if (document.getElementById("txtFcRefuel").value< 1 ){
                    document.getElementById("lblFcRefuel").innerHTML="加油数必须大于0";
                    document.getElementById("txtFcRefuel").focus();
                     return  false  ;
               }
                    document.getElementById("lblFcRefuel").innerHTML="";
               //里程表读数
                if (document.ge