jquery.validate.js

來源:互聯網
上載者:User

標籤:

jquery.validate.js使用記錄

首先到JQ官網下載搜尋validate外掛程式,下載相關源碼,附上validate官網網址

https://jqueryvalidation.org/

找到validation\demo\bootstrap,參考bootstrap與volidate結合使用的例子

改造成如上樣式

如上所示,volidate能根據rules的規則當滑鼠焦點離開輸入框後,在輸入框內添加OK或者X標誌,並且產生文位元組點添加指定DOM位置。是個挺不錯的外掛程式。

自訂規則以及提示資訊:

 jQuery.validator.addMethod("url", function(value, element,params) {
      allow any non-whitespace characters as the host part
       return this.optional( element ) || /^\d{6}$/.test( value );
      }, ‘666‘);

需要注意的是:rules中 phoneNum, password,valid_code是input中name屬性的值;

rules為教研規則,phoneNum為校正元素,required為校正方法,true為校正方法的參數;

messages:為校正錯誤是提示的字串;

errorElement:追加錯誤資訊時添加的標籤

errorPlacement:錯誤資訊追加到DOM

success:校正成功操作

highlight:添加成功樣式

unhighlight:添加失敗樣式

$( "#signupForm" ).validate( {
        rules: {
            phoneNum: {
              required: true,
              rangelength: [11, 11],
              number: true
              },
            password: {
              required: true,
              rangelength: [6, 6],
              number: true
            },
            valid_code: {
              // required: true,
              // rangelength: [4, 4],
              // number: true
              url: true
            }
          },
          messages: {
            phoneNum: {
              required: "請輸入11位元字",
              rangelength: "請輸入11位元字",
              number: "請輸入11位元字"
            },
            password: {
              required: "請輸入6位元字",
              rangelength: "請輸入6位元字",
              number: "請輸入6位元字"
            },
            valid_code: {
              required: "請輸入4位驗證碼",
              rangelength: "請輸入4位驗證碼",
              number: "請輸入4位元字驗證碼"
            },
          },
          errorElement: "em",
          errorPlacement: function ( error, element ) {
            error.addClass( "help-block" );
            element.parent().addClass( "has-feedback" );
            error.insertAfter( element );
            if ( !element.next( "span" )[ 0 ] ) {
              $( "<span class=‘glyphicon glyphicon-remove form-control-feedback‘></span>" ).insertAfter( element );
            }
          },
          success: function ( label, element ) {
            if ( !$( element ).next( "span" )[ 0 ] ) {
              $( "<span class=‘glyphicon glyphicon-ok form-control-feedback‘></span>" ).insertAfter( $( element ) );
            }
          },
          highlight: function ( element, errorClass, validClass ) {
            $( element ).parent().addClass( "has-error" ).removeClass( "has-success" );
            $( element ).next( "span" ).addClass( "glyphicon-remove" ).removeClass( "glyphicon-ok" );
          },
          unhighlight: function ( element, errorClass, validClass ) {
            $( element ).parent().addClass( "has-success" ).removeClass( "has-error" );
            $( element ).next( "span" ).addClass( "glyphicon-ok" ).removeClass( "glyphicon-remove" );
          }
        }
       );
    });

 

$( "#signupForm" ).validate( {
        rules: {
            phoneNum: {
              required: true,
              rangelength: [11, 11],
              number: true
              },
            password: {
              required: true,
              rangelength: [6, 6],
              number: true
            },
            valid_code: {
              // required: true,
              // rangelength: [4, 4],
              // number: true
              url: true
            }
          },
          messages: {
            phoneNum: {
              required: "請輸入11位元字",
              rangelength: "請輸入11位元字",
              number: "請輸入11位元字"
            },
            password: {
              required: "請輸入6位元字",
              rangelength: "請輸入6位元字",
              number: "請輸入6位元字"
            },
            valid_code: {
              required: "請輸入4位驗證碼",
              rangelength: "請輸入4位驗證碼",
              number: "請輸入4位元字驗證碼"
            },
          },
          errorElement: "em",
          errorPlacement: function ( error, element ) {
            error.addClass( "help-block" );
            element.parent().addClass( "has-feedback" );
            error.insertAfter( element );
            if ( !element.next( "span" )[ 0 ] ) {
              $( "<span class=‘glyphicon glyphicon-remove form-control-feedback‘></span>" ).insertAfter( element );
            }
          },
          success: function ( label, element ) {
            if ( !$( element ).next( "span" )[ 0 ] ) {
              $( "<span class=‘glyphicon glyphicon-ok form-control-feedback‘></span>" ).insertAfter( $( element ) );
            }
          },
          highlight: function ( element, errorClass, validClass ) {
            $( element ).parent().addClass( "has-error" ).removeClass( "has-success" );
            $( element ).next( "span" ).addClass( "glyphicon-remove" ).removeClass( "glyphicon-ok" );
          },
          unhighlight: function ( element, errorClass, validClass ) {
            $( element ).parent().addClass( "has-success" ).removeClass( "has-error" );
            $( element ).next( "span" ).addClass( "glyphicon-ok" ).removeClass( "glyphicon-remove" );
          }
        }
       );
    });

jquery.validate.js

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.