jquery.validate通过remote来实现ajax验证范例

jquery.validate可谓是个强大的表单验证插件,当然也完美支持ajax验证,这样可以方便、快速来实现检查用户名等等是否存在。


html/js源码部分:

[html]  view plain  copy
  1. <meta charset="utf-8" />  
  2. <!DOCTYPE html>    
  3. <html>    
  4.     <head>    
  5.     <title>Submit a form via AJAX</title>    
  6.   
  7. <script src="js/jquery.min.js"></script>  
  8.   
  9.     <script src="http://static.runoob.com/assets/jquery-validation-1.14.0/dist/jquery.validate.min.js"></script>  
  10.     <script src="http://static.runoob.com/assets/jquery-validation-1.14.0/dist/localization/messages_zh.js"></script>  
  11.       
  12. </head>    
  13. <body>    
  14. <form action="" name="infos" id="infos">  
  15.   
  16.     <p>用户名:<input type="text" name="username" id="username" /></p>  
  17.   
  18.     <p><input type="submit" name="sub" /></p>  
  19.   
  20. </form>  
  21.   
  22. <script type="text/javascript">  
  23.   
  24. $(function(){  
  25.     $('#infos').validate({  
  26.         debug:false,  
  27.         onkeyup:false,  
  28.         rules:{  
  29.             username:{  
  30.                 required:true,   
  31.                 remote:{  
  32.                     type:"POST",  
  33.                     url:"01.php", //请求地址  
  34.                     data:{  
  35.                         username:function(){ return $("#username").val(); }  
  36.                     }  
  37.                 }  
  38.             }  
  39.         },  
  40.         messages:{  
  41.             username:{  
  42.                 required:"用户名必填",  
  43.                 remote:"用户名已存在"  
  44.             }  
  45.         },  
  46.         submitHandler:function(form){  
  47.             alert("验证通过");  
  48.         }  
  49.     });  
  50. });  
  51.   
  52. </script>  
  53.   
  54. </body>    
  55. </html>    


@RequestMapping(value="/check")
@ResponseBody
public boolean check(HttpServletRequest request) throws Exception{
String code = request.getParameter("code").trim();
System.out.println("code = " + code);
return false;
}

猜你喜欢

转载自blog.csdn.net/gaoshan12345678910/article/details/79162010
今日推荐