我有一个简单的注册表单,使用jQuery验证来验证表单的内容.但它似乎没有工作.无论我的页面顶部是否有jQuery,我的servlet都会触发,并且表单会以无效数据提交.以下是我的代码.有人可以帮
脚本代码:
<head> <title>Sign Up</title> <meta charset="iso-8859-1"> <link rel="stylesheet" href="style/style.css" type="text/css"> <!--[if lt IE 9]><script src="scripts/html5shiv.js"></script><![endif]--> <script src="http://img.558idc.com/uploadfile/allimg/210615/2334393929-1.jpg" type="text/javascript"> </script> <script src="http://img.558idc.com/uploadfile/allimg/210615/233439D19-2.jpg" type="text/javascript"> </script> <script type="text/javascript"> $().ready(function() { // validate the comment form when it is submitted $("#signup").validate(); // validate signup form on keyup and submit $("#signup").validate({ rules: { firstname: "required", lastname: "required", address1: "required", city: "required", county: "required", postcode: "required", password_s: { required: true, minlength: 5 }, password_s_con: { required: true, minlength: 5, equalTo: "#password_s" }, email: { required: true, email: true }, agree: "required" }, messages: { firstname: "Please enter your first name", lastname: "Please enter your last name", address1: "Please enter your addres", city: "Please enter your city/town", county: "Please enter your county", postcode: "Please enter your postcode", password: { required: "Please provide a password", minlength: "Your password must be at least 5 characters long" }, confirm_password: { required: "Please provide a password", minlength: "Your password must be at least 5 characters long", equalTo: "Please enter the same password as above" }, email: "Please enter a valid email address", agree: "Please accept our policy" } }); }); </script> </head>
表格:
<table align="left"> <tr><td align="left"> <form name="signup" id="signup" action="SignUpServlet" method="post"> <table width="400px" align="right" class=""> <fieldset> <legend>Sign Up:</legend> <tr> <td>First Name:</td> <td><input type="text" id="firstname" name="firstname" required="required">*</td> </tr> <tr> <td>Last Name:</td><td><input type="text" id="lastname" name="lastname" required="required">*</td> </tr> <tr> <td>Address Line 1:</td><td> <input type="text" id="address1" name="address1" required="required">*</td> </tr> <tr> <td>Address Line 2:</td><td> <input type="text" id="address2" name="address2"></td> </tr> <tr> <td>City:</td><td> <input type="text" id="city" name="city" required="required">*</td> </tr> <tr> <td>County:</td><td><input type="text" id="county" name="county" required="required">*</td> </tr> <tr> <td>Postcode:</td> <td><input type="text" id="postcode" name="postcode" required="required">*</td> </tr> <tr> <td>Phone:</td><td> <input type="text" id="phone" name="phone"></td> </tr> <tr> <td>Email Address:</td><td> <input type="text" id="email_s" name="email_s" required="required">*</td> </tr> <tr> <td>Password:</td><td> <input type="password" id="password_s" name="password_s" required="required"></td> </tr> <tr> <td>Confirm Password:</td><td> <input type="password" id="password_s_con" name="password_s_con" required="required"></td> </tr> <tr> <td>Privacy Policy:</td><td> <input type="checkbox" class="checkbox" id="agree" name="agree" required="required"></td> </tr> <tr> <td></td><td> <input type="submit" id="lf_submit" value="submit"></td> </tr> </fieldset> </table> </form> </td></tr> <tr><td align="left"> </td></tr> </table>首先,您不需要两次调用.validate().它是多余的和多余的,所以删除第一个,并自然保留包含选项的那个.
$().ready(function() { // validate the comment form when it is submitted $("#signup").validate(); // <-- REMOVE THIS // validate signup form on keyup and submit $("#signup").validate({
在DOM中调用.validate()一次准备初始化表单.除非您在插件选项中覆盖它们,否则所有验证事件(如提交,密钥等)都是自动的.
其次,你在插件后错误地包含了jQuery.
无论您使用哪种jQuery插件,jQuery ALWAYS都需要包含在内…
<script src="http://img.558idc.com/uploadfile/allimg/210615/233439D19-2.jpg" type="text/javascript"></script> <script src="http://img.558idc.com/uploadfile/allimg/210615/2334393929-1.jpg" type="text/javascript"></script>
代码的工作演示:http://jsfiddle.net/qUYvS/
文件:http://jqueryvalidation.org/