標籤:character messages less mat 使用 snap address add 字母
Jquery Validate 相關參數及常用的自訂驗證規則
一、官網地址:http://bassistance.de/jquery-plugins/jquery-plugin-validation
二、預設校正規則
(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
三、預設的提示
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}.")},
四、Jquery Validate 自訂驗證規則
// 社會安全號碼碼驗證
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));
}, “請輸入漢字”);
// 字元最小長度驗證(一個中文字元長度為2)
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(“zipCode”, function(value, element) {
var tel = /^[0-9]{6}$/;
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}!”));
Jquery Validate 預設校正規則及常用的自訂驗證規則