BootStrap 動態添加驗證項和取消驗證項的實現方法_javascript技巧

來源:互聯網
上載者:User

bootstrap 中的bootstrapValidator可以對前端的資料進行驗證,但是有的時候我們需要動態添加驗證,這樣需要我們動態對bootstrapValidator的內容做修改。

  傳統的bootstrapValidator驗證是

$('#MaintainEntryForm').bootstrapValidator({message: '輸入值無效!',feedbackIcons: {valid: 'glyphicon glyphicon-ok',invalid: 'glyphicon glyphicon-remove',validating: 'glyphicon glyphicon-refresh'},fields: {Specifications: {validators: {notEmpty: { message: '請輸入規格!' }}},ProductNameEN: {validators: {notEmpty: { message: '請輸入英文名稱!' }}},ApplyUnit: {validators: {notEmpty: { message: '請輸入申報單位!' },stringLength: {min: 1,max: 3,message: '申報單位最多輸入3位!'}}},SuttleWeight: {validators: {notEmpty: { message: '請輸入淨重!' },//regexp: { regexp: /^\d{0,8}\.{0,1}(\d{1,3})?$/, message: '淨重只能輸入大於等於0的整數或者3位小數!' },callback: {message: '淨重只能輸入大於0的整數或者3位小數!',callback: function (value, validator, $field) {var reg = /^\d{0,8}\.{0,1}(\d{1,3})?$/;return parseFloat(value) > 0 && reg.test(value);}}}}}}).on("success.form.bv", function (e) {if (EntryInfo.SaveType == 1) {EntryInfo.SaveEntryInfo();}else if (EntryInfo.SaveType == 2) {EntryInfo.SaveAndSubmitAudit();}});

  這樣的驗證在頁面有互動的需求的時候不能夠滿足條件,所以我們需要動態對驗證條件進行修改,這裡的做法是先把所有可能用到的條件先全部添加進去,在指定條件下去除篩選條件。

  去除方式

$('#MaintainEntryForm').data('bootstrapValidator').validateField('ApplyUnit');

  這樣 名稱為ApplyUnit的驗證凡是就不會起作用了

對於已經提示報錯資訊的要去除驗證資訊的可以使用以下方式

$('#MaintainEntryForm').data('bootstrapValidator').updateStatus('ApplyQty', 'NOT_VALIDATED', null).validateField('ApplyQty');

  這樣頁面對應的ApplyQty 欄位後面驗證資訊就會消失 不用重設整個bootstrapValidator 就可以了

以上所述是小編給大家介紹的BootStrap 動態添加驗證項和取消驗證項的實現方法,希望對大家有所協助,如果大家有任何疑問請給我留言,小編會及時回複大家的。在此也非常感謝大家對雲棲社區網站的支援!

聯繫我們

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