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 管先飛 閱讀(1221) 評論(0)  編輯  收藏

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


          網站導航:
           
          主站蜘蛛池模板: 凤翔县| 皮山县| 烟台市| 仁怀市| 纳雍县| 扶沟县| 凤台县| 黑水县| 建瓯市| 兴义市| 望奎县| 岑溪市| 囊谦县| 伊金霍洛旗| 满洲里市| 沅陵县| 巴东县| 海晏县| 股票| 山西省| 肇源县| 玉山县| 安义县| 上犹县| 祁东县| 抚远县| 南皮县| 塔城市| 政和县| 仲巴县| 峨山| 申扎县| 万安县| 望谟县| 康保县| 开化县| 永新县| 高密市| 汉川市| 黄大仙区| 长泰县|