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)  編輯  收藏

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


          網站導航:
           
          主站蜘蛛池模板: 柏乡县| 峨眉山市| 大埔县| 济南市| 山阴县| 楚雄市| 邢台市| 呈贡县| 富民县| 榕江县| 托克逊县| 松阳县| 大庆市| 桦甸市| 宁安市| 龙胜| 尉犁县| 若尔盖县| 赤水市| 滕州市| 舒兰市| 榕江县| 原阳县| 宝坻区| 永州市| 内丘县| 东方市| 涟水县| 喀什市| 资中县| 明星| 荆州市| 犍为县| 屏边| 建昌县| 文登市| 东兴市| 昌都县| 鄯善县| 吉木萨尔县| 新化县|