標籤:c style class blog code java
$(document).ready(function () { /* 設定預設屬性 */ $.validator.setDefaults( { submitHandler: function (form) { form.submit(); } } ); // 字元驗證 jQuery.validator.addMethod( " stringCheck " , function (value, element) { return this .optional(element) || / ^[/u0391-/uFFE5/w]+$ / .test(value); } , " 只能包括中文字、英文字母、數字和底線 " ); // 中文字兩個位元組 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( " isMobile " , function (value, element) { var length = value.length; var mobile = / ^(((13[0-9]{1})|(15[0-9]{1}))+/d{8})$ / ; return this .optional(element) || (length == 11 && mobile.test(value)); } , " 請正確填寫您的手機號碼 " ); // 電話號碼驗證 jQuery.validator.addMethod( " isTel " , function (value, element) { var tel = / ^/d{3,4}-?/d{7,9}$ / ; // 電話號碼格式010-12345678 return this .optional(element) || (tel.test(value)); } , " 請正確填寫您的電話號碼 " ); // 聯絡電話(手機/電話皆可)驗證 jQuery.validator.addMethod( " isPhone " , function (value,element) { var length = value.length; var mobile = / ^(((13[0-9]{1})|(15[0-9]{1}))+/d{8})$ / ; var tel = / ^/d{3,4}-?/d{7,9}$ / ; return this .optional(element) || (tel.test(value) || mobile.test(value)); } , " 請正確填寫您的聯絡電話 " ); // 郵遞區號驗證 jQuery.validator.addMethod( " isZipCode " , function (value, element) { var tel = / ^[0-9]{6}$ / ; return this .optional(element) || (tel.test(value)); } , " 請正確填寫您的郵遞區號 " ); // 開始驗證 $( ‘ #submitForm ‘ ).validate( { /* 設定驗證規則 */ rules: { username: { required: true , stringCheck: true , byteRangeLength:[ 3 , 15 ] } , email: { required: true , email: true } , phone: { required: true , isPhone: true } , address: { required: true , stringCheck: true , byteRangeLength:[ 3 , 100 ] } } , /* 設定錯誤資訊 */ messages: { username: { required: " 請填寫使用者名稱 " , stringCheck: " 使用者名稱只能包括中文字、英文字母、數字和底線 " , byteRangeLength: " 使用者名稱必須在3-15個字元之間(一個中文字算2個字元) " } , email: { required: " 請輸入一個Email地址 " , email: " 請輸入一個有效Email地址 " } , phone: { required: " 請輸入您的聯絡電話 " , isPhone: " 請輸入一個有效聯絡電話 " } , address: { required: " 請輸入您的聯絡地址 " , stringCheck: " 請正確輸入您的聯絡地址 " , byteRangeLength: " 請詳實您的聯絡地址以便於我們聯絡您 " } } , /* 設定驗證觸發事件 */ focusInvalid: false , onkeyup: false , /* 設定錯誤資訊提示DOM */ errorPlacement: function (error, element) { error.appendTo( element.parent()); } , } ); } );