validate 表单验证

导入的文件

   
   
  1. <script src="js/jquery-1.8.3.js"></script>
  2. <script src="js/valid/jquery.validate.min.js"></script>
验证的时候只能同步,不能异步

将提示错误放置于指定容器中

   
   
  1. <div class="div-box">
  2. <div id="errorInfo"></div> //提示错误放置的地方
  3. <form id="formA" action="jsp/valid/testA.jsp">
  4. <div class field-box">
  5. <div class="field-text line-left">
  6. <label>姓名:</label> <input type="text" value="" name="ename">
  7. </div>
  8. <div class="field-msg line-left"></div>
  9. <div class="clearfiex"></div>
  10. </div>
  11. <div class="field-box">
  12. <div class="field-text line-left">
  13. <label>年龄:</label> <input type="number" name="age">
  14. </div>
  15. <div class="field-msg line-left"></div>
  16. <div class="clearfiex"></div>
  17. </div>
  18. <input type="submit" value="提交">
  19. </form>
  20. </div>
  21. <script type="text/javascript">
  22. $(function(){
  23. var container = $("#errorInfo"); //获取放置提示错误的
  24. $("#formA").validate({
  25. rules:{ //规则
  26. ename:"required", //设置名字为必填
  27. age:{ //设置年龄的规则
  28. required:true,
  29. range:[19,30]
  30. }
  31. },
  32. messages:{
  33. ename:{ //名字不符合规则时触发相应的mesg
  34. required:"用户名称是必须的"
  35. },
  36. age:{ //年龄不符合规则时触发相应的mesg
  37. required:"年龄是必须要填写的",
  38. range:"年龄必须是{0}到{1}之间"
  39. }
  40. },
  41. errorElement:"div", //放置错误的div
  42. errorLabelContainer: container, //放置错误的容器
  43. })
  44. })
  45. </script>

通过按钮提交

    
    
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
  2. <%
  3. String path = request.getContextPath();
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  5. %>
  6. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  7. <html>
  8. <head>
  9. <base href="<%=basePath%>">
  10. <title>My JSP 'demoA.jsp' starting page</title>
  11. <meta http-equiv="pragma" content="no-cache">
  12. <meta http-equiv="cache-control" content="no-cache">
  13. <meta http-equiv="expires" content="0">
  14. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  15. <meta http-equiv="description" content="This is my page">
  16. <!--
  17. <link rel="stylesheet" type="text/css" href="styles.css">
  18. -->
  19. <style type="text/css">
  20. .field-box{ width: 600px; border: 1px blue solid; margin-bottom: 10px}
  21. .line-left{ float: left;}
  22. .clearfiex{ clear: both;}
  23. span.error { //错误时候出现的图标
  24. background:url("images/unchecked.gif") no-repeat 0px 0px;
  25. padding-left: 16px;
  26. }
  27. span.success { //验证成功时的图标
  28. background:url("images/checked.gif") no-repeat 0px 0px;
  29. padding-left: 16px;
  30. }
  31. </style>
  32. <script src="js/jquery-1.8.3.js"></script>
  33. <script src="js/valid/jquery.validate.min.js"></script>
  34. <!-- <script src="js/valid/localization/messages_zh.min.js"></script> -->
  35. </head>
  36. <body>
  37. <div class="div-box">
  38. <form id="formA" action="jsp/valid/testA.jsp">
  39. <div class="field-box">
  40. <div class="field-text line-left">
  41. <label>姓名:</label> <input type="text" value="" name="ename">
  42. </div>
  43. <div class="field-msg line-left"></div>
  44. <div class="clearfiex"></div>
  45. </div>
  46. <div class="field-box">
  47. <div class="field-text line-left">
  48. <label>年龄:</label> <input type="number" name="age">
  49. </div>
  50. <div class="field-msg line-left"></div>
  51. <div class="clearfiex"></div>
  52. </div>
  53. <input type="submit" value="提交">
  54. <input type="button" value="提交2" onclick="submitFormX()">
  55. </form>
  56. </div>
  57. <script type="text/javascript">
  58. function submitFormX(){ //按钮提交
  59. var flag = $("#formA").valid(); // valid() 表单验证的方法 返回true或false
  60. alert(flag);
  61. if(flag)
  62. $("#formA").submit(); //提交
  63. }
  64. </script>
  65. <script type="text/javascript">
  66. $(function(){
  67. $("#formA").validate({ //规则和错误消息要一致对应
  68. debug: false,
  69. rules:{ //规则
  70. ename:"required",
  71. age:{
  72. required:true,
  73. range:[19,30]
  74. }
  75. },
  76. messages:{ //错误消息
  77. ename:{
  78. required:"用户名称是必须的"
  79. },
  80. age:{
  81. required:"年龄是必须要填写的",
  82. range:"年龄必须是{0}到{1}之间"
  83. }
  84. },
  85. validClass:'success', //修复复合条件后正确图标无法出现
  86. errorElement: "span", //将错误元素包装到span中
  87. errorPlacement: function(error, element) { //error, 错误的消息(span包装好的) element 错误的表单元素
  88. error.appendTo(element.parent().next(".field-msg")); //默认错误信息出现在表单元素的右侧,这里可以调整位置,这是将错误消息放到表单元素的下面的div中
  89. },
  90. success: function(label) { //laber是一个封装了的对象,对该对象添加验证成功的样式class,这个会出现在错误信息定义的位置
  91. label.addClass("success");
  92. }
  93. })
  94. })
  95. </script>
  96. </body>
  97. </html>

form表单中的sumbit提交

    
    
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
  2. <%
  3. String path = request.getContextPath();
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  5. %>
  6. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  7. <html>
  8. <head>
  9. <base href="<%=basePath%>">
  10. <title>My JSP 'demoA.jsp' starting page</title>
  11. <meta http-equiv="pragma" content="no-cache">
  12. <meta http-equiv="cache-control" content="no-cache">
  13. <meta http-equiv="expires" content="0">
  14. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  15. <meta http-equiv="description" content="This is my page">
  16. <!--
  17. <link rel="stylesheet" type="text/css" href="styles.css">
  18. -->
  19. <style type="text/css">
  20. .field-box{ width: 600px; border: 1px blue solid; margin-bottom: 10px}
  21. .line-left{ float: left;}
  22. .clearfiex{ clear: both;}
  23. span.error {
  24. background:url("images/unchecked.gif") no-repeat 0px 0px;
  25. padding-left: 16px;
  26. }
  27. span.success {
  28. background:url("images/checked.gif") no-repeat 0px 0px;
  29. padding-left: 16px;
  30. }
  31. </style>
  32. <script src="js/jquery-1.8.3.js"></script>
  33. <script src="js/valid/jquery.validate.min.js"></script>
  34. <!-- <script src="js/valid/localization/messages_zh.min.js"></script> -->
  35. </head>
  36. <body>
  37. <div class="div-box">
  38. <form id="formA" action="jsp/valid/testA.jsp">
  39. <div class="field-box">
  40. <div class="field-text line-left">
  41. <label>姓名:</label> <input type="text" value="" name="ename">
  42. </div>
  43. <div class="field-msg line-left"></div>
  44. <div class="clearfiex"></div>
  45. </div>
  46. <div class="field-box">
  47. <div class="field-text line-left">
  48. <label>年龄:</label> <input type="number" name="age">
  49. </div>
  50. <div class="field-msg line-left"></div>
  51. <div class="clearfiex"></div>
  52. </div>
  53. <input type="submit" value="提交">
  54. </form>
  55. </div>
  56. <script type="text/javascript">
  57. $(function(){
  58. $("#formA").validate({
  59. debug: true,
  60. rules:{
  61. ename:"required",
  62. age:{
  63. required:true,
  64. range:[19,30]
  65. }
  66. },
  67. messages:{
  68. ename:{
  69. required:"用户名称是必须的"
  70. },
  71. age:{
  72. required:"年龄是必须要填写的",
  73. range:"年龄必须是{0}到{1}之间"
  74. }
  75. },
  76. validClass:'success',
  77. errorElement: "span",
  78. errorPlacement: function(error, element) {
  79. error.appendTo(element.parent().next(".field-msg"));
  80. },
  81. success: function(label) {
  82. label.addClass("success");
  83. }
  84. })
  85. })
  86. </script>
  87. </body>
  88. </html>

 submitHandler方式提交

 submitHandler与success的区别:
 success:当表单元素验证成功时执行,对于同一个表单元素,由于onkeyup、onfocusout、onsubmit等事件都会触发验证,所以本方法可能会多次执行。
  submitHandler:当表单验证成功并提交时执行, 存在此方法时表单只能在此方法内部执行form.submit()才能提交,可理解成它替代了表单的onsubmit方法

    
    
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
  2. <%
  3. String path = request.getContextPath();
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  5. %>
  6. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  7. <html>
  8. <head>
  9. <base href="<%=basePath%>">
  10. <title>My JSP 'demoA.jsp' starting page</title>
  11. <meta http-equiv="pragma" content="no-cache">
  12. <meta http-equiv="cache-control" content="no-cache">
  13. <meta http-equiv="expires" content="0">
  14. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  15. <meta http-equiv="description" content="This is my page">
  16. <!--
  17. <link rel="stylesheet" type="text/css" href="styles.css">
  18. -->
  19. <style type="text/css">
  20. .field-box{ width: 600px; border: 1px blue solid; margin-bottom: 10px}
  21. .line-left{ float: left;}
  22. .clearfiex{ clear: both;}
  23. span.error {
  24. background:url("images/unchecked.gif") no-repeat 0px 0px;
  25. padding-left: 16px;
  26. }
  27. span.success {
  28. background:url("images/checked.gif") no-repeat 0px 0px;
  29. padding-left: 16px;
  30. }
  31. </style>
  32. <script src="js/jquery-1.8.3.js"></script>
  33. <script src="js/valid/jquery.validate.min.js"></script>
  34. <!-- <script src="js/valid/localization/messages_zh.min.js"></script> -->
  35. </head>
  36. <body>
  37. <div class="div-box">
  38. <form id="formA" action="jsp/valid/testA.jsp">
  39. <div class="field-box">
  40. <div class="field-text line-left">
  41. <label>姓名:</label> <input type="text" value="" name="ename">
  42. </div>
  43. <div class="field-msg line-left"></div>
  44. <div class="clearfiex"></div>
  45. </div>
  46. <div class="field-box">
  47. <div class="field-text line-left">
  48. <label>年龄:</label> <input type="number" name="age">
  49. </div>
  50. <div class="field-msg line-left"></div>
  51. <div class="clearfiex"></div>
  52. </div>
  53. <input type="submit" value="提交">
  54. </form>
  55. </div>
  56. <script type="text/javascript">
  57. $(function(){
  58. $("#formA").validate({
  59. debug: false,
  60. rules:{
  61. ename:"required",
  62. age:{
  63. required:true,
  64. range:[19,30]
  65. }
  66. },
  67. messages:{
  68. ename:{
  69. required:"用户名称是必须的"
  70. },
  71. age:{
  72. required:"年龄是必须要填写的",
  73. range:"年龄必须是{0}到{1}之间"
  74. }
  75. },
  76. validClass:'success',
  77. errorElement: "span",
  78. errorPlacement: function(error, element) {
  79. error.appendTo(element.parent().next(".field-msg"));
  80. },
  81. success: function(label) {
  82. label.addClass("success");
  83. },
  84. submitHandler:function(form){ //替代了表单的onsubmit  
  85. alert(form); //提交表单的处理自己ajax提交 或 只能用form.submit()
  86. }
  87. })
  88. })
  89. </script>
  90. </body>
  91. </html>

与easyui的交互:

交互的地方
easyui的ajax提交中
  1. onSubmit: function(){ //提交数据前触发,返回false 不做提交,返回true 才提交。
  2. // do some check
  3. alert("提交前触发");
  4. return $("#formA").valid(); //在表单提交时验证。validate的验证方式交互的地方
  5. } ,
代码:

    
    
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
  2. <!-- action="userServlet.do?dir=edit" -->
  3. <style type="text/css">
  4. .field-box{ width: 600px; border: 1px blue solid; margin-bottom: 10px}
  5. .line-left{ float: left;}
  6. .clearfiex{ clear: both;}
  7. span.error {
  8. background:url("images/unchecked.gif") no-repeat 0px 0px;
  9. padding-left: 16px;
  10. }
  11. span.success {
  12. background:url("images/checked.gif") no-repeat 0px 0px;
  13. padding-left: 16px;
  14. }
  15. </style>
  16. <form id="userForm" method="post">
  17. <fieldset>
  18. <legend>用户编辑</legend>
  19. <label>账户:</label> <input id="uname" type="text" value="${requestScope.user.uname}" name="uname"><br/>
  20. <label>性别:</label>
  21. <input type="radio" value="男" name="sex" ${requestScope.user.sex=="男"?"checked":""} >
  22. <input type="radio" value="女" name="sex" ${requestScope.user.sex=="女"?"checked":""}>
  23. <br/>
  24. <label>年龄:</label> <input type="number" value="${requestScope.user.age}" name="age"><br/>
  25. <input type="hidden" value="${requestScope.user.userid}" name="userid">
  26. <input type="button" value="提交" onclick="submitFormX()">
  27. </fieldset>
  28. </form>
  29. //validate的表单验证
  30. <script type="text/javascript">
  31. $(function(){
  32. $("#formA").validate({
  33. debug: true, //测试的时候用false,表示点击提交不提交,上线的时候改为
  34. rules:{
  35. ename:"required",
  36. age:{
  37. required:true,
  38. range:[19,30]
  39. }
  40. },
  41. messages:{
  42. ename:{
  43. required:"用户名称是必须的"
  44. },
  45. age:{
  46. required:"年龄是必须要填写的",
  47. range:"年龄必须是{0}到{1}之间"
  48. }
  49. },
  50. validClass:'success',
  51. errorElement: "span",
  52. errorPlacement: function(error, element) {
  53. error.appendTo(element.parent().next(".field-msg"));
  54. },
  55. success: function(label) {
  56. label.addClass("success");
  57. }
  58. })
  59. })
  60. </script>
  61. //easyui的框架ui
  62. <script type="text/javascript">
  63. function submitFormX(){
  64. //var userinfo = $("#userForm").serialize() ;
  65. // alert(userinfo);
  66. $('#userForm').form({
  67. url:'userServlet.do?dir=edit',
  68. onSubmit: function(){ //提交数据前触发,返回false 不做提交,返回true 才提交。
  69. // do some check
  70. alert("提交前触发");
  71. return $("#formA").valid(); //在表单提交时验证。validate的验证方式交互的地方
  72. } ,
  73. success:function(data){
  74. var data = eval('(' + data + ')');
  75. if(data.resultFlag){
  76. $('#userWin').window('close');
  77. $.messager.show({
  78. title:'系统操作提示',
  79. msg:'添加用户成功。',
  80. timeout:5000,
  81. showType:'slide',
  82. style:{
  83. right:'',
  84. bottom:''
  85. }
  86. });
  87. $('#userdg').datagrid('reload'); // 重新载入当前页面数据
  88. }else{
  89. }
  90. }
  91. });
  92. $('#userForm').submit();
  93. }
  94. </script>



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值