jquery表单验证插件初探
参考:
http://www.runoob.com/jquery/jquery-plugin-validate.html
http://www.cnblogs.com/liuhongfeng/p/5134648.html
使用jquery.validate.min.js插件
<script src="http://static.runoob.com/assets/jquery-validation-1.14.0/lib/jquery.js"></script>
<script src="http://static.runoob.com/assets/jquery-validation-1.14.0/dist/jquery.validate.min.js"></script>
<script src="http://static.runoob.com/assets/jquery-validation-1.14.0/dist/localization/messages_zh.js"></script>
注意: messages_zh.js这个好像是当输入不标准时候弹出的warning信息.
form表单调用js
效果:
js完整代码
<!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>
<title>jQuery PlugIn -一个简单带验证例子</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script src="http://static.runoob.com/assets/jquery-validation-1.14.0/lib/jquery.js"></script>
<script src="http://static.runoob.com/assets/jquery-validation-1.14.0/dist/jquery.validate.min.js"></script>
<script src="http://static.runoob.com/assets/jquery-validation-1.14.0/dist/localization/messages_zh.js"></script>
<style type="text/css">
* { font-family: Verdana; font-size:13px; }
input[type='text']{width:200px;}
textarea{width:155px;}
label { width: 10em; float: left; }
label.error { float: none; color: red; padding-left: .5em; vertical-align: top; }
</style>
<script>
$(document).ready(function(){
$("#commentForm").validate();
});
</script>
</head>
<body>
<form id="commentForm" method="get" action="" >
<fieldset>
<legend>表单验证</legend>
<p><label>Name</label><input name="name" class="required" maxlength="4" minlength="2" /></p>
<p><label >E-Mail</label><input name="email" class="required email" /></p>
<p><label >URL</label><input name="url" class="url"/></p>
<p><label>text</label><textarea name="text" cols="22" class="required"></textarea></p>
<p><input class="submit" type="submit" value="提交"/></p>
</fieldset>
</form>
</body>
</html>