jquery 验证插件 示例

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head >
<style type="text/css">
form.cmxform label.error, label.error { color: red; font-style: italic; } 
</style>
</head>
  <meta http-equiv="content-type" content="text/html" charset="utf-8"/>
  <script type="text/javascript" src="js/jquery-1.10.2.js"></script>
  <script type="text/javascript" src="js/jquery.validate.js"></script>
  <script type="text/javascript" >
$.validator.setDefaults({
submitHandler: function() {
alert("已经提交!");
}
});
$().ready(function() {
$("#commentForm").validate({
rules: {
name: {
required:true,
minlength:2
},
email: {
required: true,
email: true
}
},
messages: {
name: {
required:"姓名必填",
minlength:"至少2个字符"
},
email:"请输入一个邮件地址"
});
});
</script>
<body>
<form class="cmxform" id="commentForm" method="get" action="">
<legend>请输入你的姓名,电子邮件和网址,以及你的评论</legend>
<label for="cname">姓名(必填,并2个字符)</label>
<input id="cname" name="name" />
<label for="cemail">电子邮件 (必填)</label>
<input id="email" name="email" />
<label for="curl">网址 (可选)</label>
<input id="curl" name="url" value="" />
<label for="ccomment">你的评论 (必填)</label>
<textarea id="ccomment" name="comment" ></textarea>
<input class="submit" type="submit" value="提交"/>
 
</form>
</body>
</html>
 
 
例 2:
 
html 文件:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html" charset="utf-8"/>
<script type="text/javascript" src="js/jquery-1.10.2.js"></script>
  <script type="text/javascript" src="js/jquery.validate.js"></script>
  <script type="text/javascript" src="js/rules.js"></script>
<script type="text/javascript">
$(function(){
validateform(); 
 
})
 
</script>
</head>
<body>
 
    <form id="myform" name="myform"  action="" method="get">  
    <table>
    <tr>
    <td><input type="text"  name="name"  /></td>
    </tr>
 
<input type="text" name="email"  />
<input class="submit" type="submit" value="提交" />
</table>
</form>
</body>
</html>
 
 
js文件:
 
function validateform()
{
$("#myform").validate({
rules: {
name: {
required:true,
minlength:2
},
email: {
required: true,
email: true
}
},
messages: {
name: {
required:"姓名必填",
minlength:"至少2个字符"
},
email:"请输入一个邮件地址"
}
});
};

posted on 2013-12-06 09:53  小刈  阅读(148)  评论(0编辑  收藏  举报

导航