<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title></title> ...
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<script src="jquery-3.2.1.js"></script>
<script src="jquery-validation-1.17.0/dist/jquery.validate.js"></script>
<script src="jquery.metadata.js"></script>
<script src="jquery-validation-1.17.0/dist/localization/messages_zh.js"></script>
<script>
$(function () {
//$("#addForm").validate({
// rules: {
// username: {
// required: true,
// minlength: 4
// },
// email: {
// required: true,
// email: true
// },
// address: "url",
// content: "required"
// }
//});
$("#addForm").validate({
rules: {
username: { required: true, minlength: 2 },
email: { required: true, email: true },
address: "url",
content: "required",
cvalcode: { formual: "7+9" }
}
});
});
$.validator.addMethod(
"formual",
function (value, element, param) {
return value == eval(param);
},
'請輸入正確的結果'
);
</script>
</head>
<body>
<form action="#" method="post" id="addForm">
<p>
姓名:
<input type="text" name="username" value=" " />
</p>
<p>
電子郵件:
<input type="text" name="email" value=" " />
</p>
<p>
網址:
<input type="text" name="address" value=" " />
</p>
<p>
我的評論:
<textarea name="content"></textarea>
</p>
<label for="cvalcode">驗證碼</label>
<input type="text" name="cvalcode" id="cvalcode" value=" " />=7+9
<p>
<input type="submit" name="tijiao" value="提交" />
</p>
</form>
</body>
</html>
代碼詳情如下:
1 <!DOCTYPE html> 2 <html xmlns="http://www.w3.org/1999/xhtml"> 3 <head> 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 5 <title></title> 6 <script src="jquery-3.2.1.js"></script> 7 <script src="jquery-validation-1.17.0/dist/jquery.validate.js"></script> 8 <script src="jquery.metadata.js"></script> 9 <script src="jquery-validation-1.17.0/dist/localization/messages_zh.js"></script> 10 <script> 11 $(function () { 12 //$("#addForm").validate({ 13 // rules: { 14 // username: { 15 // required: true, 16 // minlength: 4 17 // }, 18 // email: { 19 // required: true, 20 // email: true 21 // }, 22 // address: "url", 23 // content: "required" 24 // } 25 //}); 26 $("#addForm").validate({ 27 rules: { 28 username: { required: true, minlength: 2 }, 29 email: { required: true, email: true }, 30 address: "url", 31 content: "required", 32 cvalcode: { formual: "7+9" } 33 } 34 }); 35 }); 36 $.validator.addMethod( 37 "formual", 38 function (value, element, param) { 39 return value == eval(param); 40 }, 41 '請輸入正確的結果' 42 ); 43 </script> 44 </head> 45 <body> 46 <form action="#" method="post" id="addForm"> 47 <p> 48 姓名: 49 <input type="text" name="username" value=" " /> 50 </p> 51 <p> 52 電子郵件: 53 <input type="text" name="email" value=" " /> 54 </p> 55 <p> 56 網址: 57 <input type="text" name="address" value=" " /> 58 </p> 59 <p> 60 我的評論: 61 <textarea name="content"></textarea> 62 </p> 63 <label for="cvalcode">驗證碼</label> 64 <input type="text" name="cvalcode" id="cvalcode" value=" " />=7+9 65 <p> 66 <input type="submit" name="tijiao" value="提交" /> 67 </p> 68 </form> 69 </body> 70 </html>View Code