jquery validate.addMethod Regex

來源:互聯網
上載者:User

標籤: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());             } ,             } );     } );

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.