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 管先飞 阅读(1212) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航: