guanxf

          我的博客:http://blog.sina.com.cn/17learning

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            71 隨筆 :: 1 文章 :: 41 評論 :: 0 Trackbacks
          一:JQuery驗證表單:
               if($("#insertDABox").attr("checked")){ //如果選擇插入編號
                     $("#saveType").attr("value","insert");
                      }else{
                      $("#saveType").attr("value","add");
                      }  
             }
               //校驗表單
            function  checkDept(){
            if($("#deptAcct").val()=="" || null==$("#deptAcct").val() ){
                         alert("請輸入單位編號");
                          $("#deptAcct").focus();
                         return false;
                     }
            if($("#deptAcct").val().length>4){
                         alert("請輸入單位編號太長");
                          $("#deptAcct").focus();
                         return false;
                     }
                     
                     if($("#deptName").val()=="" || null==$("#deptName").val() ){
                         alert("請輸入單位名稱");
                         $("#deptName").focus();
                         return false;
                      }
                     if($("#deptName").val().length>25){
                         alert("輸入的單位名稱太長");
                          $("#deptName").focus();
                         return false;
                     }
                     var regEmail=/^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/;
                     var rege = new RegExp(regEmail); 
                     if($("#deptEmail").val().length>0){
                    if(!rege.test($("#deptEmail").val())){ 
                          alert("輸入的郵件不正確!");
                          $("#deptEmail").focus();
                           return false;
                    }
                     }
                     
                     if($("#deptTel").val().length>13){
                          alert("輸入電話號碼不正確!");
                          $("#deptTel").focus();
                           return false;
                     }
          2、使用javascript封裝對象:
          (1)、封裝類:
          function validate_required(field,alerttxt){
          with (field) {
          if (value==null||value=="")
           {alert(alerttxt);return false}
          else {return true}
          }
          }
          function validate_email(field,alerttxt)
          {
          with (field)
          {
          apos=value.indexOf("@")
          dotpos=value.lastIndexOf(".")
          if (apos<1||dotpos-apos<2){
          alert(alerttxt);return false}
          else {return true}
          }
          }
          (2)、前臺調用:
          function validate_form(thisform){
          with (thisform){
            if (validate_required(issueName,"雜志名不能為空!")==false){
              issueName.focus();return false
              }
             if (validate_required(nextIssue,"下一期刊號不能為空!")==false){
              nextIssue.focus();return false
              }
          }
          return true;
            }

          3、使用Ajax表單驗證:
          (1)、jsp頁面
            $.post('${path}/deptServlet?method=insert&currentPage=${currentPage}',{
                    deptAcct:$("#deptAcct").val(),
                               deptName:$("#deptName").val()
                },function(data){
                if('0'==data){
                alert("請單位編號錯誤,請重新輸入!");
                   $("#deptAcct").focus();
                return false;
                }else{
                alert("添加成功");
                window.location.href="${path}/deptServlet?method=goToInsert";
                   }
                ///pages/rights/admin/dept/insert.jsp
                },'josn');
          (2)、java:
          sendJson(“你輸入的信息不正確!!”, resp);
          posted on 2012-09-28 15:46 管先飛 閱讀(1220) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 临汾市| 那坡县| 中西区| 晋城| 林州市| 忻城县| 加查县| 台北县| 临泉县| 永新县| 江山市| 凌海市| 阿勒泰市| 京山县| 海林市| 峨山| 衡山县| 双辽市| 常德市| 固原市| 抚顺县| 甘洛县| 怀安县| 百色市| 景泰县| 峡江县| 犍为县| 呼伦贝尔市| 清河县| 资兴市| 临高县| 内丘县| 北川| 绍兴县| 柘城县| 玉环县| 绥宁县| 左权县| 抚宁县| 水富县| 崇左市|