在JQuery的validate功能中使用remote实现Ajax功能后台验证

来源:互联网 发布:ubuntu 16.04 dash 编辑:程序博客网 时间:2024/04/30 13:28

===========A:单独使用JQuery  Ajax功能(随意返回各种类型数值)===========================

/**注册-验证手机号是否已存在*/
function validateTelID() {
 var telID = $("#telID").val();
 $.ajax({type:"POST", url:"Regedit_validateTelID", data:"telID=" + telID, success:function (data) {
  if (data == 0) {  
   //0为不存在,提交注册
  } else {
   // 验证手机号存在(1为存在,留在当前页面)
  }
 }});
}

 

==========B:在JQuery的validate功能中使用remote实现Ajax功能后台验证(只可返回String类型"true"或"false"值)===

   <script type="text/javascript">
   $.validator.setDefaults({
     submitHandler: function(regeditForm)
     {
      alert("提交ok"); 
      //document.regeditForm.action="Regedit_regeditUserStep2"
      // document.regeditForm.submit();
     }
    });
   $().ready(function() {
    $("#regeditForm").validate({
     rules: {
      confirmCode: {
       required: true,
       remote: {
        type:"POST",
        url:"Regedit_validateConfirmCode",
        data:{
        confirmCode: function() {return $("#confirmCode").val();}
        }
                   }
               },
      userPwd: {
       required: true,
       minlength: 2
      },
      confirmUserPwd: {
       required: true,
       minlength: 2,
       equalTo: "#userPwd"
      }
     },
     messages: {
      confirmCode: {
       required: "<font color=/"#ff0000/">请填写验证码</font>",
       remote:"验证码错误了"
      },
      userPwd: {
       required: "<font color=/"#ff0000/">请填写密码</font>",
       minlength: "<font color=/"#ff0000/">密码长度不得小于5位</font>"
      },
      confirmUserPwd: {
       required: "<font color=/"#ff0000/">请重复输入密码</font>",
       minlength: "<font color=/"#ff0000/">密码长度不得小于5位</font>",
       equalTo: "<font color=/"#ff0000/">确认密码与密码不一致</font>"
      }
     }
    }); 
  });
  </script>

 

 

 

b中使用remote实现ajax ,只需注意后台action只能返回"true"或"false"

前端不用进行再次判断

  HttpServletRequest request = ServletActionContext.getRequest();
  HttpServletResponse response = ServletActionContext.getResponse();
  String confirmCode = request.getParameter("confirmCode").trim();
  response.setContentType("text/html");
  String value = "false";
  response.getWriter().write(value);

原创粉丝点击