jquery.validate使用攻略 第一部
更新時(shí)間:2010年07月01日 19:35:45 作者:
好幾年不寫JS了,資料整理起來比較慢,格式也有點(diǎn)亂
主要分幾部分
jquery.validate 基本用法
jquery.validate API說明
jquery.validate 自定義
jquery.validate 常見類型的驗(yàn)證代碼
下載地址
jquery.validate插件的文檔地址
http://docs.jquery.com/Plugins/Validation
jquery.validate插件的主頁
http://bassistance.de/jquery-plugins/jquery-plugin-validation/
jquery.validate插件主頁上提供的demo
http://jquery.bassistance.de/validate/demo/
驗(yàn)證規(guī)則下面是默認(rèn)校驗(yàn)規(guī)則,也可以自定義規(guī)則
(1)required:true 必輸字段
(2)remote:"check.php" 使用ajax方法調(diào)用check.php驗(yàn)證輸入值
(3)email:true 必須輸入正確格式的電子郵件
(4)url:true 必須輸入正確格式的網(wǎng)址
(5)date:true 必須輸入正確格式的日期
(6)dateISO:true 必須輸入正確格式的日期(ISO),例如:2009-06-23,1998/01/22 只驗(yàn)證格式,不驗(yàn)證有效性
(7)number:true 必須輸入合法的數(shù)字(負(fù)數(shù),小數(shù))
(8)digits:true 必須輸入整數(shù)
(9)creditcard: 必須輸入合法的信用卡號(hào)
(10)equalTo:"#field" 輸入值必須和#field相同
(11)accept: 輸入擁有合法后綴名的字符串(上傳文件的后綴)
(12)maxlength:5 輸入長度最多是5的字符串(漢字算一個(gè)字符)
(13)minlength:10 輸入長度最小是10的字符串(漢字算一個(gè)字符)
(14)rangelength:[5,10] 輸入長度必須介于 5 和 10 之間的字符串")(漢字算一個(gè)字符)
(15)range:[5,10] 輸入值必須介于 5 和 10 之間
(16)max:5 輸入值不能大于5
(17)min:10 輸入值不能小于10
驗(yàn)證提示
下面是默認(rèn)的驗(yàn)證提示,官網(wǎng)有簡體中文版的驗(yàn)證提示下載,或者通過jQuery.extend(jQuery.validator.messages自定義錯(cuò)誤提示信息,可以將網(wǎng)站的驗(yàn)證提示文本統(tǒng)一到一個(gè)文件里。
required: "This field is required.",
remote: "Please fix this field.",
email: "Please enter a valid email address.",
url: "Please enter a valid URL.",
date: "Please enter a valid date.",
dateISO: "Please enter a valid date (ISO).",
number: "Please enter a valid number.",
digits: "Please enter only digits",
creditcard: "Please enter a valid credit card number.",
equalTo: "Please enter the same value again.",
accept: "Please enter a value with a valid extension.",
maxlength: $.validator.format("Please enter no more than {0} characters."),
minlength: $.validator.format("Please enter at least {0} characters."),
rangelength: $.validator.format("Please enter a value between {0} and {1} characters long."),
range: $.validator.format("Please enter a value between {0} and {1}."),
max: $.validator.format("Please enter a value less than or equal to {0}."),
min: $.validator.format("Please enter a value greater than or equal to {0}.")
使用方式
1:
在控件中使用默認(rèn)驗(yàn)證規(guī)則,例子:
電子郵件(必填) <input id="email" class="required email" value="email@" />2:
可以在控件中自定義驗(yàn)證規(guī)則,例子:
自定義(必填,[3,5])
<input id="complex" value="hi" class="{required:true,minlength:3, maxlength:5,
messages:{required:'為什么不輸入一點(diǎn)文字呢',minlength:'輸入的太少了',maxlength:'輸入那么多干嘛'}}" />
3: 通過javascript自定義驗(yàn)證規(guī)則,下面的JS自定義了兩個(gè)規(guī)則,password和confirm_password
$().ready(function() {
$("#form2").validate({
rules: {
password: {
required: true,
minlength: 5
},
confirm_password: {
required: true,
minlength: 5,
equalTo: "#password"
}
},
messages: {
password: {
required: "沒有填寫密碼",
minlength: jQuery.format("密碼不能小于{0}個(gè)字符")
},
confirm_password: {
required: "沒有確認(rèn)密碼",
minlength: "確認(rèn)密碼不能小于{0}個(gè)字符",
equalTo: "兩次輸入密碼不一致嘛"
}
}
});
});
required除了設(shè)置為true/false之外,還可以使用表達(dá)式或者函數(shù),比如
$("#form2").validate({
rules: {
funcvalidate: {
required: function() {return $("#password").val()!=""; }
}
},
messages: {
funcvalidate: {
required: "有密碼的情況下必填"
}
}
});
Html
密碼<input id="password" name="password" type="password" />
確認(rèn)密碼<input id="confirm_password" name="confirm_password" type="password" />
條件驗(yàn)證<input id="funcvalidate" name="funcvalidate" value="" />
4: 使用meta自定義驗(yàn)證信息
首先用JS設(shè)置meta
$("#form3").validate({ meta: "validate" });
Html
email<input class="{validate:{required:true, email:true,
messages:{required:'輸入email地址', email:'你輸入的不是有效的郵件地址'}}}"/>
5: 使用meta可以將驗(yàn)證規(guī)則寫在自定義的標(biāo)簽內(nèi),比如validate
JS設(shè)置meta
$().ready(function() {
$.metadata.setType("attr", "validate");
$("#form1").validate();
});
Html
Email
<input id="email" name="email"
validate="{required:true, email:true, messages:{required:'輸入email地址', email:'你輸入的不是有效的郵件地址'}}" />
6: 自定義驗(yàn)證規(guī)則
對(duì)于復(fù)雜的驗(yàn)證,可以通過jQuery.validator.addMethod添加自定義的驗(yàn)證規(guī)則
官網(wǎng)提供的additional-methods.js里包含一些常用的驗(yàn)證方式,比如lettersonly,ziprange,nowhitespace等
例子
// 字符驗(yàn)證
jQuery.validator.addMethod("userName", function(value, element) {
return this.optional(element) || /^[\u0391-\uFFE5\w]+$/.test(value);
}, "用戶名只能包括中文字、英文字母、數(shù)字和下劃線");
//然后就可以使用這個(gè)規(guī)則了
$("#form1").validate({
// 驗(yàn)證規(guī)則
rules: {
userName: {
required: true,
userName: true,
rangelength: [5,10]
}
},
/* 設(shè)置錯(cuò)誤信息 */
messages: {
userName: {
required: "請(qǐng)?zhí)顚懹脩裘?,
rangelength: "用戶名必須在5-10個(gè)字符之間"
}
},
});
7: radio、checkbox、select的驗(yàn)證方式類似
radio的驗(yàn)證
性別
<span>
男<input type="radio" id="gender_male" value="m" name="gender" class="{required:true}"/><br />
女<input type="radio" id="gender_female" value="f" name="gender" />
</span>
checkbox的驗(yàn)證
最少選擇兩項(xiàng)
<span>
選項(xiàng)1<input type="checkbox" id="check_1" value="1" name="checkGroup"
class="{required:true,minlength:2, messages:{required:'必須選擇',minlength:'至少選擇2項(xiàng)'}}" /><br />
選項(xiàng)2<input type="checkbox" id="check_2" value="2" name="checkGroup" /><br />
選項(xiàng)3<input type="checkbox" id="check_3" value="3" name="checkGroup" /><br />
</span>
select的驗(yàn)證
下拉框
<span>
<select id="selectbox" name="selectbox" class="{required:true}">
<option value=""></option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
</span>
8: Ajax驗(yàn)證
用remote可以進(jìn)行Ajax驗(yàn)證
remote: {
url: "url", //url地址
type: "post", //發(fā)送方式
dataType: "json", //數(shù)據(jù)格式 data: { //要傳遞的數(shù)據(jù)
username: function() {
return $("#username").val();
}}
}
補(bǔ)充: jQuery Validation插件remote驗(yàn)證方式的Bug
http://www.dbjr.com.cn/article/24079.htm
下一章是API的具體說明
然后整理怎么進(jìn)一步自定義jQuery.validate以及網(wǎng)上一些常用的驗(yàn)證代碼
jquery.validate 基本用法
jquery.validate API說明
jquery.validate 自定義
jquery.validate 常見類型的驗(yàn)證代碼
下載地址
jquery.validate插件的文檔地址
http://docs.jquery.com/Plugins/Validation
jquery.validate插件的主頁
http://bassistance.de/jquery-plugins/jquery-plugin-validation/
jquery.validate插件主頁上提供的demo
http://jquery.bassistance.de/validate/demo/
驗(yàn)證規(guī)則下面是默認(rèn)校驗(yàn)規(guī)則,也可以自定義規(guī)則
(1)required:true 必輸字段
(2)remote:"check.php" 使用ajax方法調(diào)用check.php驗(yàn)證輸入值
(3)email:true 必須輸入正確格式的電子郵件
(4)url:true 必須輸入正確格式的網(wǎng)址
(5)date:true 必須輸入正確格式的日期
(6)dateISO:true 必須輸入正確格式的日期(ISO),例如:2009-06-23,1998/01/22 只驗(yàn)證格式,不驗(yàn)證有效性
(7)number:true 必須輸入合法的數(shù)字(負(fù)數(shù),小數(shù))
(8)digits:true 必須輸入整數(shù)
(9)creditcard: 必須輸入合法的信用卡號(hào)
(10)equalTo:"#field" 輸入值必須和#field相同
(11)accept: 輸入擁有合法后綴名的字符串(上傳文件的后綴)
(12)maxlength:5 輸入長度最多是5的字符串(漢字算一個(gè)字符)
(13)minlength:10 輸入長度最小是10的字符串(漢字算一個(gè)字符)
(14)rangelength:[5,10] 輸入長度必須介于 5 和 10 之間的字符串")(漢字算一個(gè)字符)
(15)range:[5,10] 輸入值必須介于 5 和 10 之間
(16)max:5 輸入值不能大于5
(17)min:10 輸入值不能小于10
驗(yàn)證提示
下面是默認(rèn)的驗(yàn)證提示,官網(wǎng)有簡體中文版的驗(yàn)證提示下載,或者通過jQuery.extend(jQuery.validator.messages自定義錯(cuò)誤提示信息,可以將網(wǎng)站的驗(yàn)證提示文本統(tǒng)一到一個(gè)文件里。
復(fù)制代碼 代碼如下:
required: "This field is required.",
remote: "Please fix this field.",
email: "Please enter a valid email address.",
url: "Please enter a valid URL.",
date: "Please enter a valid date.",
dateISO: "Please enter a valid date (ISO).",
number: "Please enter a valid number.",
digits: "Please enter only digits",
creditcard: "Please enter a valid credit card number.",
equalTo: "Please enter the same value again.",
accept: "Please enter a value with a valid extension.",
maxlength: $.validator.format("Please enter no more than {0} characters."),
minlength: $.validator.format("Please enter at least {0} characters."),
rangelength: $.validator.format("Please enter a value between {0} and {1} characters long."),
range: $.validator.format("Please enter a value between {0} and {1}."),
max: $.validator.format("Please enter a value less than or equal to {0}."),
min: $.validator.format("Please enter a value greater than or equal to {0}.")
使用方式
1:
在控件中使用默認(rèn)驗(yàn)證規(guī)則,例子:
電子郵件(必填) <input id="email" class="required email" value="email@" />2:
可以在控件中自定義驗(yàn)證規(guī)則,例子:
自定義(必填,[3,5])
<input id="complex" value="hi" class="{required:true,minlength:3, maxlength:5,
messages:{required:'為什么不輸入一點(diǎn)文字呢',minlength:'輸入的太少了',maxlength:'輸入那么多干嘛'}}" />
3: 通過javascript自定義驗(yàn)證規(guī)則,下面的JS自定義了兩個(gè)規(guī)則,password和confirm_password
復(fù)制代碼 代碼如下:
$().ready(function() {
$("#form2").validate({
rules: {
password: {
required: true,
minlength: 5
},
confirm_password: {
required: true,
minlength: 5,
equalTo: "#password"
}
},
messages: {
password: {
required: "沒有填寫密碼",
minlength: jQuery.format("密碼不能小于{0}個(gè)字符")
},
confirm_password: {
required: "沒有確認(rèn)密碼",
minlength: "確認(rèn)密碼不能小于{0}個(gè)字符",
equalTo: "兩次輸入密碼不一致嘛"
}
}
});
});
required除了設(shè)置為true/false之外,還可以使用表達(dá)式或者函數(shù),比如
復(fù)制代碼 代碼如下:
$("#form2").validate({
rules: {
funcvalidate: {
required: function() {return $("#password").val()!=""; }
}
},
messages: {
funcvalidate: {
required: "有密碼的情況下必填"
}
}
});
Html
密碼<input id="password" name="password" type="password" />
確認(rèn)密碼<input id="confirm_password" name="confirm_password" type="password" />
條件驗(yàn)證<input id="funcvalidate" name="funcvalidate" value="" />
4: 使用meta自定義驗(yàn)證信息
首先用JS設(shè)置meta
$("#form3").validate({ meta: "validate" });
Html
email<input class="{validate:{required:true, email:true,
messages:{required:'輸入email地址', email:'你輸入的不是有效的郵件地址'}}}"/>
5: 使用meta可以將驗(yàn)證規(guī)則寫在自定義的標(biāo)簽內(nèi),比如validate
JS設(shè)置meta
$().ready(function() {
$.metadata.setType("attr", "validate");
$("#form1").validate();
});
Html
<input id="email" name="email"
validate="{required:true, email:true, messages:{required:'輸入email地址', email:'你輸入的不是有效的郵件地址'}}" />
6: 自定義驗(yàn)證規(guī)則
對(duì)于復(fù)雜的驗(yàn)證,可以通過jQuery.validator.addMethod添加自定義的驗(yàn)證規(guī)則
官網(wǎng)提供的additional-methods.js里包含一些常用的驗(yàn)證方式,比如lettersonly,ziprange,nowhitespace等
例子
復(fù)制代碼 代碼如下:
// 字符驗(yàn)證
jQuery.validator.addMethod("userName", function(value, element) {
return this.optional(element) || /^[\u0391-\uFFE5\w]+$/.test(value);
}, "用戶名只能包括中文字、英文字母、數(shù)字和下劃線");
//然后就可以使用這個(gè)規(guī)則了
$("#form1").validate({
// 驗(yàn)證規(guī)則
rules: {
userName: {
required: true,
userName: true,
rangelength: [5,10]
}
},
/* 設(shè)置錯(cuò)誤信息 */
messages: {
userName: {
required: "請(qǐng)?zhí)顚懹脩裘?,
rangelength: "用戶名必須在5-10個(gè)字符之間"
}
},
});
7: radio、checkbox、select的驗(yàn)證方式類似
radio的驗(yàn)證
性別
<span>
男<input type="radio" id="gender_male" value="m" name="gender" class="{required:true}"/><br />
女<input type="radio" id="gender_female" value="f" name="gender" />
</span>
checkbox的驗(yàn)證
最少選擇兩項(xiàng)
<span>
選項(xiàng)1<input type="checkbox" id="check_1" value="1" name="checkGroup"
class="{required:true,minlength:2, messages:{required:'必須選擇',minlength:'至少選擇2項(xiàng)'}}" /><br />
選項(xiàng)2<input type="checkbox" id="check_2" value="2" name="checkGroup" /><br />
選項(xiàng)3<input type="checkbox" id="check_3" value="3" name="checkGroup" /><br />
</span>
select的驗(yàn)證
下拉框
<span>
<select id="selectbox" name="selectbox" class="{required:true}">
<option value=""></option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
</span>
8: Ajax驗(yàn)證
用remote可以進(jìn)行Ajax驗(yàn)證
remote: {
url: "url", //url地址
type: "post", //發(fā)送方式
dataType: "json", //數(shù)據(jù)格式 data: { //要傳遞的數(shù)據(jù)
username: function() {
return $("#username").val();
}}
}
補(bǔ)充: jQuery Validation插件remote驗(yàn)證方式的Bug
http://www.dbjr.com.cn/article/24079.htm
下一章是API的具體說明
然后整理怎么進(jìn)一步自定義jQuery.validate以及網(wǎng)上一些常用的驗(yàn)證代碼
您可能感興趣的文章:
- jQuery.Validate 使用筆記(jQuery Validation范例 )
- 使用jQuery.Validate進(jìn)行客戶端驗(yàn)證(初級(jí)篇) 不使用微軟驗(yàn)證控件的理由
- jquery.validate使用攻略 第二部
- jquery.validate使用攻略 第三部
- jquery.validate使用攻略 第五步 正則驗(yàn)證
- jQuery.validate 常用方法及需要注意的問題
- jQuery.Validate驗(yàn)證庫的使用介紹
- 使用jquery.validate自定義方法實(shí)現(xiàn)"手機(jī)號(hào)碼或者固話至少填寫一個(gè)"的邏輯驗(yàn)證
- 基于Bootstrap+jQuery.validate實(shí)現(xiàn)Form表單驗(yàn)證
- jquery.validate使用詳解
相關(guān)文章
輕松實(shí)現(xiàn)jQuery添加刪除按鈕Click事件
這篇文章主要為大家詳細(xì)介紹了如何輕松實(shí)現(xiàn)jQuery添加刪除按鈕Click事件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03jquery 實(shí)時(shí)監(jiān)聽輸入框值變化的完美方法(必看)
下面小編就為大家?guī)硪黄猨query 實(shí)時(shí)監(jiān)聽輸入框值變化的完美方法(必看)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-01-01js jquery獲取隨機(jī)生成id的服務(wù)器控件的三種方法
由于ASP.NET網(wǎng)頁運(yùn)行后,服務(wù)器控件會(huì)隨機(jī)生成客戶端id,jquery獲取時(shí)候不太好操作,下面為大家整理了三種方法,感興趣的朋友可以參考下哈,希望對(duì)大家有所幫助2013-07-07基于Jquery與WebMethod投票功能實(shí)現(xiàn)代碼
基于Jquery與WebMethod投票功能實(shí)現(xiàn)代碼,需要的朋友可以參考下。2011-01-01jquery checkbox 勾選的bug問題解決方案與分析
本文首先由一個(gè)在項(xiàng)目中遇到的jquery checkbox 勾選的bug的解決方案,引申出jQuery中attr()和prop()的差異分析,非常的實(shí)用,需要的小伙伴快來研究下吧2014-11-11jquery.picsign圖片標(biāo)注組件實(shí)例詳解
這篇文章主要介紹了jquery.picsign 自己封裝的第一個(gè)開源組件,需要的朋友可以參考下2018-02-02jQuery插件scroll實(shí)現(xiàn)無縫滾動(dòng)效果
今天給大家分享一款頁面無縫滾動(dòng)的jquery.scroll插件。 支持上下左右,淡入淡出,滾動(dòng)時(shí)間設(shè)置,動(dòng)畫時(shí)間設(shè)置,鼠標(biāo)經(jīng)過是否停止設(shè)置,滾動(dòng)鼠標(biāo)液動(dòng)條看下頁面的切換效果。該插件適用瀏覽器:IE8、360、FireFox、Chrome、Safari、Opera、傲游、搜狗、世界之窗。2015-04-04