【轉】基於jquery,bootstrap資料驗證外掛程式bootstrapValidator 教程

來源:互聯網
上載者:User

標籤:

bootstrap:能夠增加相容性的強大架構.

因為項目需要資料驗證,看bootstrapValidator 還不錯,就上手一直,完美相容,話不多說。

需要引用css:

bootstrap.min.css

bootstrapValidator.min.css

js:

jquery-1.10.2.min.js

bootstrap.min.js

bootstrapValidator.min.js

(下載執行個體)

以上這些都是必須的。

先上個簡單的例子,只要匯入相應的檔案可以直接運行:

 View Code

這是最基本的,例子直接複製到本地,並且匯入需要的css和js檔案(JS中username,password等索引值名和input標籤中name屬性值對應),運行就能夠進行非空,長度驗證,完全不需要管css樣式。

如下:

當然,以上都是外掛程式寫好的規則,如果想自己加匹配規則怎麼辦呢?

如下只要在input相對應的索引值中加入一個regexp:{}索引值對(在上面的js基礎上修改)

username: {/*鍵名和input name值對應*/                    message: ‘The username is not valid‘,                    validators: {                        notEmpty: {/*非空提示*/                            message: ‘使用者名稱不可為空‘                        },                        regexp: {/* 只需加此索引值對,包含Regex,和提示 */                            regexp: /^[a-zA-Z0-9_\.]+$/,                            message: ‘只能是數字和字母_.‘                        },                        stringLength: {/*長度提示*/                            min: 6,                            max: 30,                            message: ‘使用者名稱長度必須在6到30之間‘                        }/*最後一個沒有逗號*/                    }                },

效果如下:

 

 至此只要運行和看了例子,就能進行大部分的驗證了,是不是很簡單?只要寫相應的索引值對即可,再也自己什麼都寫了。下面進一步的使用,進行使用者的註冊,

需求:

即時驗證使用者名稱是否存在,密碼不能和使用者名稱相同,兩次密碼需要相同,提交之後需要驗證傳回值

html代碼(直接替換上例子中的form即可):

<form id="defaultForm" role="form" class="form-signin" action="registerAccount.do"                method="post">                <h2 class="form-signin-heading">請輸入註冊資訊:</h2>                <div class="form-group">                    <label for="username">使用者名稱:</label><input class="form-control"                        type="text" name="username" id="username" />                </div>                <div class="form-group">                    <label for="password">密碼:</label><input class="form-control"                        type="password" name="password" id="password"/>                </div>                <div class="form-group">                    <label for="repassword">確認密碼:</label><input class="form-control"                        type="password" name="repassword" id="repassword" />                </div>                <div class="form-group">                    <label for="phone">手機號碼:</label><input class="form-control"                        type="text" name="phone" id="phone" />                </div>                <div class="form-group">                    <label for="email">email:</label><input class="form-control"                        type="email" name="email" id="email" />                </div>                <div class="form-group">                    <label for="invite">邀請碼:</label><input class="form-control"                        type="text" name="invite" id="invite">                </div>                <div class="form-group">                        <button class="btn btn-lg btn-primary btn-block" type="submit">確認註冊</button>                        <a class="btn btn-lg btn-primary btn-block" href="../">返回首頁</a>                    </div>            </form>

js代碼(直接替換例子中的JS):

$(function(){/* 文檔載入,執行一個函數*/     $(‘#defaultForm‘)     .bootstrapValidator({         message: ‘This value is not valid‘,         feedbackIcons: {/*input狀態樣式圖片*/             valid: ‘glyphicon glyphicon-ok‘,             invalid: ‘glyphicon glyphicon-remove‘,             validating: ‘glyphicon glyphicon-refresh‘         },         fields: {/*驗證:規則*/             username: {//驗證input項:驗證規則                 message: ‘The username is not valid‘,                                 validators: {                     notEmpty: {//非空驗證:提示訊息                         message: ‘使用者名稱不可為空‘                     },                     stringLength: {                         min: 6,                         max: 30,                         message: ‘使用者名稱長度必須在6到30之間‘                     },                     threshold :  6 , //有6字元以上才發送ajax請求,(input中輸入一個字元,外掛程式會向伺服器發送一次,設定限制,6字元以上才開始)                     remote: {//ajax驗證。server result:{"valid",true or false} 向服務發送當前input name值,獲得一個json資料。例表示正確:{"valid",true}                           url: ‘exist2.do‘,//驗證地址                         message: ‘使用者已存在‘,//提示訊息                         delay :  2000,//每輸入一個字元,就發ajax請求,伺服器壓力還是太大,設定2秒發送一次ajax(預設輸入一個字元,提交一次,伺服器壓力太大)                         type: ‘POST‘//請求方式                         /**自訂提交資料,預設值提交當前input value                          *  data: function(validator) {                               return {                                   password: $(‘[name="passwordNameAttributeInYourForm"]‘).val(),                                   whatever: $(‘[name="whateverNameAttributeInYourForm"]‘).val()                               };                            }                          */                     },                     regexp: {                         regexp: /^[a-zA-Z0-9_\.]+$/,                         message: ‘使用者名稱由數字字母底線和.組成‘                     }                 }             },             password: {                 message:‘密碼無效‘,                 validators: {                     notEmpty: {                         message: ‘密碼不可為空‘                     },                     stringLength: {                         min: 6,                         max: 30,                         message: ‘使用者名稱長度必須在6到30之間‘                     },                     identical: {//相同                         field: ‘password‘, //需要進行比較的input name值                         message: ‘兩次密碼不一致‘                     },                     different: {//不能和使用者名稱相同                         field: ‘username‘,//需要進行比較的input name值                         message: ‘不能和使用者名稱相同‘                     },                     regexp: {                         regexp: /^[a-zA-Z0-9_\.]+$/,                         message: ‘The username can only consist of alphabetical, number, dot and underscore‘                     }                 }             },             repassword: {                 message: ‘密碼無效‘,                 validators: {                     notEmpty: {                         message: ‘使用者名稱不可為空‘                     },                     stringLength: {                         min: 6,                         max: 30,                         message: ‘使用者名稱長度必須在6到30之間‘                     },                     identical: {//相同                         field: ‘password‘,                         message: ‘兩次密碼不一致‘                     },                     different: {//不能和使用者名稱相同                         field: ‘username‘,                         message: ‘不能和使用者名稱相同‘                     },                     regexp: {//匹配規則                         regexp: /^[a-zA-Z0-9_\.]+$/,                         message: ‘The username can only consist of alphabetical, number, dot and underscore‘                     }                 }             },             email: {                 validators: {                     notEmpty: {                         message: ‘郵件不可為空‘                     },                     emailAddress: {                         message: ‘請輸入正確的郵件地址如:[email protected]‘                     }                 }             },             phone: {                 message: ‘The phone is not valid‘,                 validators: {                     notEmpty: {                         message: ‘手機號碼不可為空‘                     },                     stringLength: {                         min: 11,                         max: 11,                         message: ‘請輸入11位手機號碼‘                     },                     regexp: {                         regexp: /^1[3|5|8]{1}[0-9]{9}$/,                         message: ‘請輸入正確的手機號碼‘                     }                 }             },             invite: {                 message: ‘邀請碼‘,                 validators: {                     notEmpty: {                         message: ‘邀請碼不可為空‘                     },                     stringLength: {                         min: 8,                         max: 8,                         message: ‘請輸入正確長度的邀請碼‘                     },                     regexp: {                         regexp: /^[\w]{8}$/,                         message: ‘請輸入正確的邀請碼(包含數字字母)‘                     }                 }             },         }     })     .on(‘success.form.bv‘, function(e) {//點擊提交之後         // Prevent form submission         e.preventDefault();         // Get the form instance         var $form = $(e.target);         // Get the BootstrapValidator instance         var bv = $form.data(‘bootstrapValidator‘);         // Use Ajax to submit form data 提交至form標籤中的action,result自訂         $.post($form.attr(‘action‘), $form.serialize(), function(result) {//do something...});     });});

 

異常:

Uncaught RangeError: Maximum call stack size exceedede

沒有加class="form-group"

【轉】基於jquery,bootstrap資料驗證外掛程式bootstrapValidator 教程

聯繫我們

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