一、官網地址:http://bassistance.de/jquery-plugins/jquery-plugin-validation
二、默認校驗規則
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
(1) required:
true
必輸字段
(2) remote:
"remote-valid.jsp"
使用ajax方法調用remote-valid.jsp驗證輸入值
(3) email:
true
必須輸入正確格式的電子郵件
(4) url:
true
必須輸入正確格式的網址
(5) date:
true
必須輸入正確格式的日期,日期校驗ie6出錯,慎用
(6) dateISO:
true
必須輸入正確格式的日期(ISO),例如:2009-06-23,1998/01/22 只驗證格式,不驗證有效性
(7) number:
true
必須輸入合法的數字(負數,小數)
(8) digits:
true
必須輸入整數
(9) creditcard:
true
必須輸入合法的信用卡號
(10) equalTo:
"#password"
輸入值必須和#password相同
(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
|
三、默認的提示
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
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)."
,
dateDE:
"Bitte geben Sie ein g眉ltiges Datum ein."
,
number:
"Please enter a valid number."
,
numberDE:
"Bitte geben Sie eine Nummer ein."
,
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}."
)
},
|
示例:
$("#fm").validate({
rules:{
loginpwd:{
required:true,
minlength:6,
maxlength:12,
regex:"^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,10}$"
},
loginpwd2:{
required:true,
equalTo: "#loginpwd",
regex:"^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,10}$"
}
},
messages:{
loginpwd:{
required: "請輸入密碼",
minlength: "密碼長度不能小於 6 個字母",
regex:"密碼必須包含大小寫字母和數字的組合,不能使用特殊字符,長度在8-10之間"
},
loginpwd2:{
required: "請輸入密碼",
minlength: "密碼長度不能小於 6 個字母",
equalTo: "兩次密碼輸入不一致",
regex:"密碼必須包含大小寫字母和數字的組合,不能使用特殊字符,長度在8-10之間"
}
}
});
四、Jquery Validate 自定義驗證規則
addMethod(name,method,message)方法
參數 name 是添加的方法的名字。
參數 method 是一個函數,接收三個參數 (value,element,param) 。
value 是元素的值,element 是元素本身,param 是參數。
身份證號碼驗證
jQuery.validator.addMethod(“idcardno”, function(value, element) {
return this.optional(element) || isIdCardNo(value);
}, “請正確輸入身份證號碼”);
字母數字
jQuery.validator.addMethod(“alnum”, function(value, element) {
return this.optional(element) || /^[a-zA-Z0-9]+$/.test(value);
}, “只能包括英文字母和數字”);
郵政編碼驗證
jQuery.validator.addMethod(“zipcode”, function(value, element) {
var tel = /^[0-9]{6}$/;
return this.optional(element) || (tel.test(value));
}, “請正確填寫郵政編碼”);
漢字
jQuery.validator.addMethod(“chcharacter”, function(value, element) {
var tel = /^[u4e00-u9fa5]+$/;
return this.optional(element) || (tel.test(value));
}, “請輸入漢字”);
jQuery.validator.addMethod(“stringMinLength”, function(value, element, param) {
var length = value.length;
for(var i = 0; i < value.length; i++) {
if(value.charCodeAt(i) > 127) {
length++;
}
}
return this.optional(element) || (length >= param);
}, $.validator.format(“長度不能小於 { 0 }!”));
字符最大長度驗證(一個中文字符長度為2)
jQuery.validator.addMethod(“stringMaxLength”, function(value, element, param) {
var length = value.length;
for(var i = 0; i < value.length; i++) {
if(value.charCodeAt(i) > 127) {
length++;
}
}
return this.optional(element) || (length <= param);
}, $.validator.format(“長度不能大於 { 0 }!”));
字符驗證
jQuery.validator.addMethod(“string”, function(value, element) {
return this.optional(element) || /^[u0391-uFFE5w]+$/.test(value);
}, “不允許包含特殊符號!”);
手機號碼驗證
jQuery.validator.addMethod(“mobile”, function(value, element) {
var length = value.length;
return this.optional(element) || (length == 11 && /^(((13[0-9]{1})|(15[0-9]{1}))+d{8})$/.test(value));
}, “手機號碼格式錯誤!”);
電話號碼驗證
jQuery.validator.addMethod(“phone”, function(value, element) {
var tel = /^(d{3,4}-?)?d{7,9}$/g;
return this.optional(element) || (tel.test(value));
}, “電話號碼格式錯誤!”);
必須以特定字符串開頭驗證
jQuery.validator.addMethod(“begin”, function(value, element, param) {
var begin = new RegExp(“ ^ ”+param);
return this.optional(element) || (begin.test(value));
}, $.validator.format(“必須以 { 0 } 開頭!”));
驗證兩次輸入值是否不相同
jQuery.validator.addMethod(“notEqualTo”, function(value, element, param) {
return value != $(param).val();
}, $.validator.format(“兩次輸入不能相同!”));
驗證值不允許與特定值等於
jQuery.validator.addMethod(“notEqual”, function(value, element, param) {
return value != param;
}, $.validator.format(“輸入值不允許為 { 0 }!”));
驗證值必須大於特定值(不能等於)
jQuery.validator.addMethod(“gt”, function(value, element, param) {
return value > param;
}, $.validator.format(“輸入值必須大於 { 0 }!”));
小數點前最多9位,小數點后最多6位
jQuery.validator.addMethod("decimal", function (value, element) {
return this.optional(element) || /^([1-9]\d{0,8}|0)(\.\d{1,6})?$/.test(value);
}, "小數點前最多9位,小數點后最多6位^_^");
結束時間不能小於開始時間
jQuery.validator.addMethod("laterTo", function (value, element, param) {
return $(param).val().split("-").join("") < $(element).val().split("-").join("");
}, "結束時間不能小於開始時間^_^");

