jQuery validate驗證插件使用詳解
Validate驗證插件,內(nèi)置豐富的驗證規(guī)則,還有靈活的自定義規(guī)則接口,HTML、CSS與JS之間的低耦合能讓您自由布局和豐富樣式,支持input,select,textarea的驗證。
Description
瀏覽器支持:IE7+ 、Chrome、Firefox、Safari、Mobile Browser
jQuery版本:1.7.0+
Usage
載入jQuery、validate
<script type="text/javascript" src="jquery-1.11.1.js"></script>
<script type="text/javascript" src="jquery-validate.js"></script>
DOM標簽驗證規(guī)則填寫
<div class="form_control"> <input class="required" value="315359131@qq.com" type="text" name="email" data-tip="請輸入您的郵箱" data-valid="isNonEmpty||isEmail" data-error="email不能為空||郵箱格式不正確"> </div> <div class="form_control"> <select class="required" data-valid="isNonEmpty" data-error="省份必填"> <option value="">請選擇省份</option> <option value="001">001</option> <option value="002">002</option> </select> </div>
給需要驗證的表單元素的class填入required(不建議在這個class上做其他樣式)。
建議input用獨立div包裹,因為驗證的message是從當(dāng)前input的父元素上append生成。
data-tip:在尚未驗證而獲取焦點時出現(xiàn)的提示。
data-valid:驗證規(guī)則,若有組合驗證,以||符號分割。
data-error:驗證錯誤提示,對應(yīng)data-valid,以||符號分割。
單選/復(fù)選比較特殊,需要添加元素包裹單選/復(fù)選集合,并在包裹元素上加驗證規(guī)則。
<div class="form_control"> <span class="required" data-valid="isChecked" data-error="性別必選" data-type="radio"> <label><input type="radio" name="sex">男</label> <label><input type="radio" name="sex">女</label> <label><input type="radio" name="sex">未知</label> </span> </div> <div class="form_control"> <span class="required" data-valid="isChecked" data-error="標簽至少選擇一項" data-type="checkbox"> <label><input type="checkbox" name="label">紅</label> <label><input type="checkbox" name="label">綠</label> <label><input type="checkbox" name="label">藍</label> </span> </div>
JS調(diào)用
//**注意:必須以表單元素調(diào)用validate**
$('form').validate({
type:{
isChecked: function(value, errorMsg, el) {
var i = 0;
var $collection = $(el).find('input:checked');
if (!$collection.length) {
return errorMsg;
}
}
},
onFocus: function() {
this.parent().addClass('active');
return false;
},
onBlur: function() {
var $parent = this.parent();
var _status = parseInt(this.attr('data-status'));
$parent.removeClass('active');
if (!_status) {
$parent.addClass('error');
}
return false;
}
});

表單提交前的驗證
$('form').on('submit', function(event) {
event.preventDefault();
$(this).validate('submitValidate'); //return true or false;
});
validate內(nèi)置驗證規(guī)則
required:true 必輸字段
remote:"check.php" 使用ajax方法調(diào)用check.php驗證輸入值
email:true 必須輸入正確格式的電子郵件
url:true 必須輸入正確格式的網(wǎng)址
date:true 必須輸入正確格式的日期
dateISO:true 必須輸入正確格式的日期(ISO),例如:2009-06-23,1998/01/22 只驗證格式,不驗證有效性
number:true 必須輸入合法的數(shù)字(負數(shù),小數(shù))
digits:true 必須輸入整數(shù)
creditcard: 必須輸入合法的信用卡號
equalTo:"#field" 輸入值必須和#field相同
accept: 輸入擁有合法后綴名的字符串(上傳文件的后綴)
maxlength:5 輸入長度最多是5的字符串(漢字算一個字符)
minlength:10 輸入長度最小是10的字符串(漢字算一個字符)
rangelength:[5,10] 輸入長度必須介于 5 和 10 之間的字符串")(漢字算一個字符)
range:[5,10] 輸入值必須介于 5 和 10 之間
max:5 輸入值不能大于5
min:10 輸入值不能小于10
例子:
驗證用戶名,密碼,確認密碼,主頁,生日,郵箱等
首先引入Jquery、引入jquery.validate.js、引入messages_cn.js并且為表單定義一個id,為需要驗證的控件定義name屬性,并賦值,此插件使用的是控件的name屬性,而非id。
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="jquery郵箱驗證.aspx.cs" Inherits="練習(xí).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 runat="server">
<title></title>
<style type="text/css">
#aa{ color:Red;}
</style>
<script src="Jquery1.7.js" type="text/javascript"></script>
<script src="jquery.validate.js" type="text/javascript"></script>
<script src="messages_cn.js" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
$('#form1').validate({
rules: {
username: { required: true, minlength: 6, maxlength: 12 },
password: { required: true, minlength: 6 },
passwordok:{required: true, equalTo: "#password"},
index: { required: true, url: true },
birthday: { required: true, dateISO: true },
bloodpress:{required: true,digits:true},
email: { required: true, email: true }
},
errorshow: function (error, element) {
error.appendTo(element.siblings('span'));
}
})
})
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr><td>用戶名:</td><td>
<input name="username" type="text" /><span id="aa">*</span></td></tr>
<tr><td>密碼:</td><td>
<input id="password" name="password" type="text" /><span id="aa">*</span></td></tr>
<tr><td>確認密碼:</td><td>
<input id="repassword" name="passwordok" type="text" /><span id="aa">*</span></td></tr>
<tr><td>主頁:</td><td>
<input name="index" type="text" /><span id="aa">*</span></td></tr>
<tr><td>生日:</td><td>
<input name="birthday" type="text" /><span id="aa">*</span></td></tr>
<tr><td>血壓:</td><td>
<input name="bloodpress" type="text" /><span id="aa">*</span></td></tr>
<tr><td>郵箱:</td><td><input name="email" type="text" /><span id="aa">*</span></td></tr>
<tr><td></td><td>
<input id="Button1" type="button" value="button" /></td></tr>
</table>
</div>
</form>
</body>
</html>
實現(xiàn)如下效果:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助。
相關(guān)文章
jQuery使用ajax跨域獲取數(shù)據(jù)的簡單實例
下面小編就為大家?guī)硪黄猨Query使用ajax跨域獲取數(shù)據(jù)的簡單實例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-05-05
基于jQuery的message插件實現(xiàn)右下角彈出消息框
有時在頁面加載的時候,需要在頁面的右下角彈出一個小的提示框,顯示一些提示信息給用戶,通過使用jQuery的message插件,可以很方便的實現(xiàn)這個效果,在使用之前先介紹一下message插件中的方法的使用。2011-01-01
完美解決jQuery fancybox ie 無法顯示關(guān)閉按鈕的問題
下面小編就為大家?guī)硪黄昝澜鉀QjQuery fancybox ie 無法顯示關(guān)閉按鈕的問題。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-11-11
jquery網(wǎng)頁元素拖拽插件效果及實現(xiàn)
效果說明:配合已有css樣式,載入插件后,網(wǎng)頁元素可以隨意在窗口內(nèi)拖拽,設(shè)置了原位置半透明和拖拽半透明的效果選項,可根據(jù)需要選擇。另外,當(dāng)頁面上有多個可拖拽元素時,可以載入另外一個用于設(shè)置z-index的插件,模擬windows窗口點擊置頂效果。2013-08-08

