最近在做一個用戶註冊登錄的頁面,資料查尋過程中發現了一個非常不錯的客戶端驗證的極品-jQuery.validate。
它是著名的JavaScript包jQuery的一個插件,其實它還有其它的一些插件應該都爽,有待慢慢來學習
官方地址:http://bassistance.de/jquery-plugins/jquery-plugin-validation/
jQuery用戶手冊:http://jquery.org.cn/visual/cn/index.xml
開發使用起來非常簡單明瞭,
我的代碼:
- $(document).ready(function(){
- /* 設置默認屬性 */
- $.validator.setDefaults({
- submitHandler: function(form) { form.submit(); }
- });
- // 中文字兩個字節
- jQuery.validator.addMethod("byteRangeLength", 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[0] && length <= param[1] );
- }, "請確保輸入的值在3-15個字節之間(一箇中文字算2個字節)");
- /* 追加自定義驗證方法 */
- // 身份證號碼驗證
- jQuery.validator.addMethod("isIdCardNo", function(value, element) {
- return this.optional(element) || isIdCardNo(value);
- }, "請正確輸入您的身份證號碼");
- // 字符驗證
- jQuery.validator.addMethod("userName", function(value, element) {
- return this.optional(element) || /^[/u0391-/uFFE5/w]+$/.test(value);
- }, "用戶名只能包括中文字、英文字母、數字和下劃線");
- // 手機號碼驗證
- jQuery.validator.addMethod("isMobile", 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("isPhone", function(value, element) {
- var tel = /^(/d{3,4}-?)?/d{7,9}$/g;
- return this.optional(element) || (tel.test(value));
- }, "請正確填寫您的電話號碼");
- // 郵政編碼驗證
- jQuery.validator.addMethod("isZipCode", function(value, element) {
- var tel = /^[0-9]{6}$/;
- return this.optional(element) || (tel.test(value));
- }, "請正確填寫您的郵政編碼");
- $(regFrom).validate({
- /* 設置驗證規則 */
- rules: {
- userName: {
- required: true,
- userName: true,
- byteRangeLength: [3,15]
- },
- password: {
- required: true,
- minLength: 5
- },
- repassword: {
- required: true,
- minLength: 5,
- equalTo: "#password"
- },
- question: {
- required: true
- },
- answer: {
- required: true
- },
- realName: {
- required: true
- },
- cardNumber: {
- isIdCardNo: true
- },
- mobilePhone: {
- isMobile: true
- },
- phone: {
- isPhone: true
- },
- email: {
- required: true,
- email: true
- },
- zipCode: {
- isZipCode:true
- }
- },
- /* 設置錯誤信息 */
- messages: {
- userName: {
- required: "請填寫用戶名",
- byteRangeLength: "用戶名必須在3-15個字符之間(一箇中文字算2個字符)"
- },
- password: {
- required: "請填寫密碼",
- minlength: jQuery.format("輸入{0}.")
- },
- repassword: {
- required: "請填寫確認密碼",
- equalTo: "兩次密碼輸入不相同"
- },
- question: {
- required: "請填寫您的密碼提示問題"
- },
- answer: {
- required: "請填寫您的密碼提示答案"
- },
- realName: {
- required: "請填寫您的真實姓名"
- },
- email: {
- required: "請輸入一個Email地址",
- email: "請輸入一個有效的Email地址"
- }
- },
- /* 錯誤信息的顯示位置 */
- errorPlacement: function(error, element) {
- error.appendTo( element.parent() );
- },
- /* 驗證通過時的處理 */
- success: function(label) {
- // set as text for IE
- label.html(" ").addClass("checked");
- },
- /* 獲得焦點時不驗證 */
- focusInvalid: false,
- onkeyup: false
- });
- // 輸入框獲得焦點時,樣式設置
- $('input').focus(function(){
- if($(this).is(":text") || $(this).is(":password"))
- $(this).addClass('focus');
- if ($(this).hasClass('have_tooltip')) {
- $(this).parent().parent().removeClass('field_normal').addClass('field_focus');
- }
- });
- // 輸入框失去焦點時,樣式設置
- $('input').blur(function() {
- $(this).removeClass('focus');
- if ($(this).hasClass('have_tooltip')) {
- $(this).parent().parent().removeClass('field_focus').addClass('field_normal');
- }
- });
- });
$(document).ready(function(){
/* 設置默認屬性 */
$.validator.setDefaults({
submitHandler: function(form) { form.submit(); }
});
// 中文字兩個字節
jQuery.validator.addMethod("byteRangeLength", 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[0] && length <= param[1] );
}, "請確保輸入的值在3-15個字節之間(一箇中文字算2個字節)");
/* 追加自定義驗證方法 */
// 身份證號碼驗證
jQuery.validator.addMethod("isIdCardNo", function(value, element) {
return this.optional(element) || isIdCardNo(value);
}, "請正確輸入您的身份證號碼");
// 字符驗證
jQuery.validator.addMethod("userName", function(value, element) {
return this.optional(element) || /^[/u0391-/uFFE5/w]+$/.test(value);
}, "用戶名只能包括中文字、英文字母、數字和下劃線");
// 手機號碼驗證
jQuery.validator.addMethod("isMobile", 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("isPhone", function(value, element) {
var tel = /^(/d{3,4}-?)?/d{7,9}$/g;
return this.optional(element) || (tel.test(value));
}, "請正確填寫您的電話號碼");
// 郵政編碼驗證
jQuery.validator.addMethod("isZipCode", function(value, element) {
var tel = /^[0-9]{6}$/;
return this.optional(element) || (tel.test(value));
}, "請正確填寫您的郵政編碼");
$(regFrom).validate({
/* 設置驗證規則 */
rules: {
userName: {
required: true,
userName: true,
byteRangeLength: [3,15]
},
password: {
required: true,
minLength: 5
},
repassword: {
required: true,
minLength: 5,
equalTo: "#password"
},
question: {
required: true
},
answer: {
required: true
},
realName: {
required: true
},
cardNumber: {
isIdCardNo: true
},
mobilePhone: {
isMobile: true
},
phone: {
isPhone: true
},
email: {
required: true,
email: true
},
zipCode: {
isZipCode:true
}
},
/* 設置錯誤信息 */
messages: {
userName: {
required: "請填寫用戶名",
byteRangeLength: "用戶名必須在3-15個字符之間(一箇中文字算2個字符)"
},
password: {
required: "請填寫密碼",
minlength: jQuery.format("輸入{0}.")
},
repassword: {
required: "請填寫確認密碼",
equalTo: "兩次密碼輸入不相同"
},
question: {
required: "請填寫您的密碼提示問題"
},
answer: {
required: "請填寫您的密碼提示答案"
},
realName: {
required: "請填寫您的真實姓名"
},
email: {
required: "請輸入一個Email地址",
email: "請輸入一個有效的Email地址"
}
},
/* 錯誤信息的顯示位置 */
errorPlacement: function(error, element) {
error.appendTo( element.parent() );
},
/* 驗證通過時的處理 */
success: function(label) {
// set as text for IE
label.html(" ").addClass("checked");
},
/* 獲得焦點時不驗證 */
focusInvalid: false,
onkeyup: false
});
// 輸入框獲得焦點時,樣式設置
$('input').focus(function(){
if($(this).is(":text") || $(this).is(":password"))
$(this).addClass('focus');
if ($(this).hasClass('have_tooltip')) {
$(this).parent().parent().removeClass('field_normal').addClass('field_focus');
}
});
// 輸入框失去焦點時,樣式設置
$('input').blur(function() {
$(this).removeClass('focus');
if ($(this).hasClass('have_tooltip')) {
$(this).parent().parent().removeClass('field_focus').addClass('field_normal');
}
});
});
網上的資料有人說,它跟prototype包會有衝突,我還沒有同時使用過,這點不是很清楚,但我是發現一個問題:
對於最小/大長度的驗證方法,作者可能考慮到大家的命名習慣不同,同時做了minLength和minlength(maxLength和maxlength)方法,應該哪一個都是可以的,但對於用戶Message來說,只能夠定義針對minlength(maxlength),才能調用用戶自定義的Message,
否則只是調用包的默認Message,但具體原因還沒有查清楚。同時,這個插件提供了本地化的消息,但對於我這裏初學者來說,怎麼使用它還有待摸索!