jquery.validate.js的remote用法

 1 <script>
 2   $(function(){
 3    $("#myform").validate(
 4    {
 5     
 6     rules: {
 7      name:{required:true,rangelength:[6,20],
 8         remote:{                                          //验证用户名是否存在
 9                type:"POST",
10                url:"loginVerifyAction",             //servlet
11                data:{
12                  name:function(){return $("#name").val();}
13                } 
14               } 
15             },
16      password: {required:true,minlength:6},
17      repassword: {required:true,equalTo:"#password"},
18      veryCode: {required:true,
19         remote:{
20                type:"POST",
21                url:"valCodeAction",             
22                data:{
23                  veryCode:function(){return $("#veryCode").val();}
24                } 
25               } 
26      }
27     },
28     messages: {
29      name:{required:"用户名不能为空!",rangelength:jQuery.format("用户名位数必须在{0}到{1}字符之间!"),remote:jQuery.format("用户名已经被注册")},
30      password: {required:"密码不能为空!",minlength:jQuery.format("密码位数必须大于等于6个字符!")},
31      repassword: {required:"确认密码不能为空!",equalTo:"确认密码和密码不一致!"},
32      veryCode:  {required:"请输入验证码",remote:jQuery.format("验证码错误")}
33     }
34    });
35   });
36  
37  </script>
38 
39  
40 
41 servlet代码:
42 
43 //验证用户名是否存在
44 
45 public class LoginVerifyAction  extends HttpServlet {
46  public void service(HttpServletRequest request, HttpServletResponse response)
47    throws IOException, ServletException {
48 
49   response.reset();
50   response.setContentType("text/html;charset=UTF-8");
51  //业务逻辑操作countByParams得到值并存储到num中  
52    if(num!=0){
53       response.getWriter().print(false);                                
54      }
55      else{
56       response.getWriter().print(true);
57      }
58  }
59 }
60 
61 //验证验证码
62 
63 public class ValCodeAction extends HttpServlet {
64 
65  public void service(HttpServletRequest request, HttpServletResponse response)
66    throws IOException, ServletException {
67   //得到验证码的操作请看另一篇文章 :验证码
68         response.setContentType("text/html;charset=UTF-8");
69   String validateC = request.getSession().getAttribute("validateCode").toString().trim();  
70         String veryCode = request.getParameter("veryCode").trim();
71         if(veryCode.equals(validateC)){
72          response.getWriter().print(true);
73         }else{
74          response.getWriter().print(false);
75         }
76 }
77 
78 xml中的配置 : 忽略。。。

 

posted @ 2016-12-13 15:05  涤新云  阅读(8732)  评论(0编辑  收藏  举报