jquery.validate使用攻略 第一部
2024-09-06 12:45:30
供稿:網友
主要分幾部分
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自定義錯誤提示信息,可以將網站的驗證提示文本統一到一個文件里。
代碼如下:
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:
在控件中使用默認驗證規則,例子:
電子郵件(必填) <input id="email" class="required email" value="email@" />2: