• 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中的配置 : 忽略。。。
  • 相关阅读:
    推荐阅读20100603
    [ASP.NET4之旅]Circular file references are not allowed
    满园尽是503,记曾经的一次IIS 7性能考验
    VS2010小Bug:找不到System.Web.Extensions.dll引用
    VS2010奇异Bug:三个中文符号在CSS文件中轻松让VS2010崩溃
    推荐阅读20100528
    Windows平台网站文件同步备份解决方案——cwRsyn
    推荐阅读20100803
    在IE8中使用建行企业网银的解决方法
    博客园已经用上NorthScale Memcached Server
  • 原文地址:https://www.cnblogs.com/dixinyunpan/p/6170031.html
Copyright © 2020-2023  润新知