jquery.vlidate的remote用法

<script type="text/javascript">
   
    $.validator.setDefaults({
	submitHandler: function() { form.submit(); }
});

$().ready(function() {

  $("#form").validate({
     rules:
     {
     username:{
	     remote: {                             
      		url: "/checkun",//验证地址,这里用的是servlet可以用jsp或者php
      		type:'POST',//大写
      		dataType:'json',
     		data:{ username:function(){
				return $('#username').val()
				}
	}
      
     },required: true,minlength:4},
     password:{required: true,minlength:4,maxlength:16},
     password2:{required: true,minlength:4,equalTo:"#password"}
     },
     messages:
     {
     username:{required: "请输入用户名",minlength: "用户名至少四位数",remote:"用户名已经存在"},
     password:{required: "请输入密码",minlength: "密码长度为4~16",maxlength: "不超过16位"},
     password2:{required: "重复密码",minlength:"长度为4~16",maxlength: "不超过16位",equalTo: "两次输入不同"}
     }

     }); 

  });


  <body>
   <form id="form" name="form" method="post" action="user.do?method=adduser">
   <div id="main">
     <p>注册</p>
     <p> 
     <label for="username">用户名</label>
     <input class="c" name="username" type="text" id="username"  onblur="validatorloginName();" />
     </p><div id="checkresult" ></div>
     <p>
     <label for="password">密码</label>
      <input class="c" name="password" type="password" id="password"   />
     </p>
     <p>
     <label for="password2">重复密码</label>
     <input class="c" name="password2" type="password" id="password2"  />
     </p>
     <p>
     <input type="Submit" name="Submit" value="提交" id="sub" onclick="check();"  />
     <input type="reset" name="reset" value="重置" id="sub" />
     </p>   
   </div>
   </form>
  </body>


下面是servlet
package servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import daoimlp.UserInfoImpl;

public class Validate extends HttpServlet {

	
	public Validate() {
		super();
	}

	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		response.setCharacterEncoding("UTF8");
		String username = request.getParameter("username").toString();
		UserInfoImpl userimpl = new UserInfoImpl();
		System.out.println("dovalidate");
		PrintWriter out = response.getWriter();
		if(userimpl.cheakUser(username))
		{
		out.println("true");//只能是true和false 如果不行的话就把双引号去了
		System.out.print("1");
		}else{
			out.println("false");
			System.out.print("2");
		}
		out.close();
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
			this.doGet(request, response);
	}
	public void init() throws ServletException {
		// Put your code here
	}

}


你可能感兴趣的:(jquery,Ajax,验证,vlidate,remote)