主要分幾部分
jquery.validate 基本用法
jquery.validate API說明
jquery.validate 自定義
jquery.validate 常見類型的驗證代碼
下載地址
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/
驗證規則下面是默認校驗規則,也可以自定義規則
(1)required:true 必輸字段
(2)remote:"check.php" 使用ajax方法調用check.php驗證輸入值
(3)email:true 必須輸入正確格式的電子郵件
(4)url:true 必須輸入正確格式的網址
(5)date:true 必須輸入正確格式的日期
(6)dateISO:true 必須輸入正確格式的日期(ISO),例如:2009-06-23,1998/01/22 只驗證格式,不驗證有效性
(7)number:true 必須輸入合法的數字(負數,小數)
(8)digits:true 必須輸入整數
(9)creditcard: 必須輸入合法的信用卡號
(10)equalTo:"#field" 輸入值必須和#field相同
(11)accept: 輸入擁有合法后綴名的字符串(上傳文件的后綴)
(12)maxlength:5 輸入長度最多是5的字符串(漢字算一個字符)
(13)minlength:10 輸入長度最小是10的字符串(漢字算一個字符)
(14)rangelength:[5,10] 輸入長度必須介于 5 和 10 之間的字符串")(漢字算一個字符)
(15)range:[5,10] 輸入值必須介于 5 和 10 之間
(16)max:5 輸入值不能大于5
(17)min:10 輸入值不能小于10
驗證提示
下面是默認的驗證提示,官網有簡體中文版的驗證提示下載,或者通過jQuery.extend(jQuery.validator.messages自定義錯誤提示信息,可以將網站的驗證提示文本統一到一個文件里。
代碼如下:

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

使用方式
1:
在控件中使用默認驗證規則,例子:
電子郵件(必填) <input id="email" class="required email" value="email@" />2:
可以在控件中自定義驗證規則,例子:
自定義(必填,[3,5])
<input id="complex" value="hi" class="{required:true,minlength:3, maxlength:5,
messages:{required:'為什么不輸入一點文字呢',minlength:'輸入的太少了',maxlength:'輸入那么多干嘛'}}" />
3: 通過javascript自定義驗證規則,下面的JS自定義了兩個規則,password和confirm_password
代碼如下:

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

Html
密碼<input id="password" name="password" type="password" />
確認密碼<input id="confirm_password" name="confirm_password" type="password" />
條件驗證<input id="funcvalidate" name="funcvalidate" value="" />
4: 使用meta自定義驗證信息
首先用JS設置meta
$("#form3").validate({ meta: "validate" });
Html
email<input class="{validate:{required:true, email:true,
messages:{required:'輸入email地址', email:'你輸入的不是有效的郵件地址'}}}"/>
5: 使用meta可以將驗證規則寫在自定義的標簽內,比如validate
JS設置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: 自定義驗證規則
對于復雜的驗證,可以通過jQuery.validator.addMethod添加自定義的驗證規則
官網提供的additional-methods.js里包含一些常用的驗證方式,比如lettersonly,ziprange,nowhitespace等
例子
代碼如下:

2

3

4

5

6

7

8

9

10

11

12

13

7: radio、checkbox、select的驗證方式類似
radio的驗證
性別
<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的驗證
最少選擇兩項
<span>
選項1<input type="checkbox" id="check_1" value="1" name="checkGroup"
class="{required:true,minlength:2, messages:{required:'必須選擇',minlength:'至少選擇2項'}}" /><br />
選項2<input type="checkbox" id="check_2" value="2" name="checkGroup" /><br />
選項3<input type="checkbox" id="check_3" value="3" name="checkGroup" /><br />
</span>
select的驗證
下拉框
<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驗證
用remote可以進行Ajax驗證
remote: {
url: "url", //url地址
type: "post", //發送方式
dataType: "json", //數據格式 data: { //要傳遞的數據
username: function() {
return $("#username").val();
}}
}
補充: jQuery Validation插件remote驗證方式的Bug
http://www.jb51.net/article/24079.htm
下一章是API的具體說明
然后整理怎么進一步自定義jQuery.validate以及網上一些常用的驗證代碼
詳細出處參考:http://www.jb51.net/article/24078.htm