我一直试图验证此表格:
<table>
<form action="test.html" method="post" id="contactform" name="contactform">
<tr>
<td>
<label for="fname">First Name :</label>
</td>
<td>
<input name="fname" id="fname" type="text" />
</td>
</tr>
<tr>
<td>
<label for="lname">Last Name :</label>
</td>
<td>
<input name="lname" id="lname" type="text" />
</td>
</tr>
<tr>
<td>
<label for="gender">Gender :</label>
</td>
<td>
<input name="gender" id="genderm" type="radio" /> Male <input name="gender" id="genderf" type="radio" /> Female
</td>
</tr>
<tr>
<td>
<label for="email">Email :</label>
</td>
<td>
<input name="email" id="email" type="text" />
</td>
</tr>
<tr>
<td>
<label for="address">Address :</label>
</td>
<td>
<input name="addr" id="addr" type="text" />
</td>
</tr>
<tr>
<td>
<label for="mob">Mobile Phone :</label>
</td>
<td>
<input name="mob" id="mob" type="text" />
</td>
</tr>
<tr>
<td>
<label for="dob">Date Of Birth (mm/dd/YYYY):</label>
</td>
<td>
<input name="dob" id="dob" type="text" />
</td>
</tr>
<tr>
<td>
<label for="agreeT">By Checking This you agree to our Terms :</label>
</td>
<td>
<input name="agreeT" id="agreeT" type="checkbox" />
</td>
</tr>
<tr>
<td>
<input type="submit" value="Sumbit" />
</td>
</tr>
但我无法使用Jquery Validation Plugin验证它我在我的文件中添加这些脚本代码:
<script src="jquery.js"></script>
<script src="jquery.validate.js"></script>
<script language="javascript">
$(document).ready(function(e) {
var validator = $("#contactform").validate({
rules : {
fname: "required",
lname: "required",
email: {
email: true,
required: true
},
gender:" required",
addr: "required",
dob: {
required: true,
date: true
},
agreeT: "required"
}
}
);
});
</script>
任何人都可以告诉我为什么我要去test.html而不验证表格.... ??有没有语法错误??
答案 0 :(得分:2)
工作代码..已检查:: ---
试试: -
要遵循的步骤: -
您的Html代码: -
<form action="test.html" method="post" id="contactform" name="contactform">
<table>
<tr>
<td>
<label for="fname">First Name :</label>
</td>
<td>
<input name="fname" id="fname" type="text" />
</td>
</tr>
<tr>
<td>
<label for="lname">Last Name :</label>
</td>
<td>
<input name="lname" id="lname" type="text" />
</td>
</tr>
<tr>
<td>
<label for="gender">Gender :</label>
</td>
<td>
<input name="gender" id="genderm" type="radio" /> Male <input name="gender" id="genderf" type="radio" /> Female
</td>
</tr>
<tr>
<td>
<label for="email">Email :</label>
</td>
<td>
<input name="email" id="email" type="text" />
</td>
</tr>
<tr>
<td>
<label for="address">Address :</label>
</td>
<td>
<input name="addr" id="addr" type="text" />
</td>
</tr>
<tr>
<td>
<label for="mob">Mobile Phone :</label>
</td>
<td>
<input name="mob" id="mob" type="text" />
</td>
</tr>
<tr>
<td>
<label for="dob">Date Of Birth (mm/dd/YYYY):</label>
</td>
<td>
<input name="dob" id="dob" type="text" />
</td>
</tr>
<tr>
<td>
<label for="agreeT">By Checking This you agree to our Terms :</label>
</td>
<td>
<input name="agreeT" id="agreeT" type="checkbox" />
</td>
</tr>
<tr>
<td>
<input type="submit" value="Sumbit" />
</td>
</tr> </table>
</form>
你的js代码: -
<script type="text/javascript">
$().ready(function() {
$("#contactform").validate({
rules : {
fname: "required",
lname: "required",
email: {
email: true,
required: true
},
gender:" required",
addr: "required",
dob: {
required: true,
date: true
},
agreeT: "required"
},
messages: {
fname: "Please enter your name",
lname: "Please enter your last name",
email: "Please enter a valid email address"
}
});
});
</script>
------------ OR -------------------
<script language="javascript">
$(document).ready(function() {
$("#contactform").validate({
rules : {
fname: {required: true},
lname: {required: true},
email: {
email: true,
required: true
},
gender:{required: true},
addr: {required: true},
dob: {
required: true,
date: true
},
agreeT:{required: true}
},
messages: {
fname: "Please enter your name",
lname: "Please enter your last name",
email: "Please enter a valid email address"
}
}); });
答案 1 :(得分:0)
将<form..>
移出<table>
像...
<form ...>
<table>
...
</table>
</form>
希望它有效