jQuery.validate验证时用的是Form输入表单的name属性

来源:互联网 发布:mac抹掉磁盘重装黑屏 编辑:程序博客网 时间:2024/05/21 11:33

例如:

[html] view plain copy
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  2. <html>  
  3. <head>  
  4. <title>test</title>  
  5. <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />  
  6. <meta http-equiv="Cache-Control" content="no-store" />  
  7. <meta http-equiv="Pragma" content="no-cache" />  
  8. <meta http-equiv="Expires" content="0" />  
  9.    
  10. <link href="/static/jquery-easyui/1.2.6/themes/default/easyui.css"  
  11.     type="text/css" rel="stylesheet">  
  12. <link href="/static/jquery-easyui/1.2.6/themes/icon.css"  
  13.     type="text/css" rel="stylesheet">  
  14. <link href="/static/style/demo.css" type="text/css"  
  15.     rel="stylesheet">  
  16. <link href="/static/jquery-validation/1.9.0/milk.css"  
  17.     type="text/css" rel="stylesheet" />  
  18.    
  19. <script src="/static/jquery/1.7.2/jquery.min.js"  
  20.     type="text/javascript"></script>  
  21. <script src="/static/jquery-easyui/1.2.6/jquery.easyui.min.js"  
  22.     type="text/javascript"></script>  
  23. <script  
  24.     src="/static/jquery-validation/1.9.0/jquery.validate.min.js"  
  25.     type="text/javascript"></script>  
  26. <script src="/static/jquery-validation/1.9.0/messages_cn.js"  
  27.     type="text/javascript"></script>  
  28.    
  29. <script>    
  30.   
  31. $(document).ready(function() {  
  32.     $("#inputForm").validate({  
  33.         rules : {  
  34.                 name:{  
  35.                     required:true,  
  36.                     minlength:"3"     
  37.                 },    
  38.                 password:{  
  39.                     minlength:"3"     
  40.                 },    
  41.                 passwordConfirm:{  
  42.                     equalTo : "#password"  
  43.                 },  
  44.                 email:{  
  45.                     required:true,  
  46.                     email:true    
  47.                 }         
  48.         },  
  49.         messages : {  
  50.   
  51.         }  
  52.     });  
  53. });  
  54. </script>  
  55. <title>PLMII:用户管理</title>  
  56. </head>  
  57. <body>  
  58.     <form id="inputForm" action="user!save.action" method="post">   
  59.     <table align="left">  
  60.         <tr align="left">  
  61.         <td nowrap><label for="name" class="field">姓名:</label></td>  
  62.         <td><input  type="text" id="name" name="name" size="25" value=""/></td>     
  63.         <td nowrap><label for="email" class="field">邮箱:</label></td>  
  64.         <td><input  type="text" id="email" name="email"    size="25" value=""/></td>    
  65.         </tr>  
  66.             <tr align="left">                   
  67.         <td nowrap><label for="password" class="field">密码:</label></td>  
  68.         <td><input type="password" id="password" name="password" size="26" value="" /></td>  
  69.         <td nowrap><label for="passwordConfirm" class="field">确认密码:</label></td>  
  70.         <td><input type="password" id="passwordConfirm" size="26" value="" /></td>                      
  71.        </tr>  
  72.            <tr>  
  73.                <td colspan=4 align="center">  
  74.                    <a href="#"class="easyui-linkbutton" iconCls="icon-ok"onclick='$("#inputForm").submit()'>提交</a>   
  75.                    <a href="#" class="easyui-linkbutton" iconCls="icon-cancel"onclick="history.back(-1)">取消</a>  
  76.                </td>  
  77.            </tr>  
  78.        </table>  
  79.     </form>  
  80. </body>  
  81. </html>  

在这段代码中,不会验证两个密码是否相同,因为确认密码输入框只定义了其id,而未定义其name,将其name加上,则验证正常。

[html] view plain copy
  1. <tr align="left">                   
  2.     <td nowrap><label for="password" class="field">password:</label></td>  
  3.     <td><input type="password" id="password" name="password" size="26" value="" /></td>  
  4.     <td nowrap><label for="passwordConfirm" class="field">确认password:</label></td>  
  5.     <td><input type="password" id="passwordConfirm" name="passwordConfirm" size="26" value="" /></td>                   
  6. </tr>  
阅读全文
0 0