欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

基于jQuery 實(shí)現(xiàn)bootstrapValidator下的全局驗(yàn)證

 更新時(shí)間:2015年12月07日 15:31:08   作者:chua1989  
這篇文章主要介紹了基于jQuery 實(shí)現(xiàn)bootstrapValidator下的全局驗(yàn)證 的相關(guān)資料,需要的朋友可以參考下

BootstrapValidator 是一款專門針對(duì)Boostrap v3的表單檢驗(yàn)jQuery插件,能夠?qū)崿F(xiàn)眾多常用的檢驗(yàn)功能,并且易于擴(kuò)展,還支持中文!對(duì)于bootstrap用戶來說能夠開箱即用。

前置:

  引入jQuery、bootstrap、bootstrapValidator

  問題描述:

  項(xiàng)目中要求所有的表單輸入框中都不能輸入&符號(hào)。沒有在bootstrap中找到有方法可用,只能自己動(dòng)手了

思路:

  使用正則。

  分兩種情況,第一種,如果輸入框有自身的正則驗(yàn)證則不用去管(一般來說使用正則驗(yàn)證是嚴(yán)格控制輸入的);第二種,如果沒有正則則需要添加不能輸入&的正則。

  需要重載bootstrapValidator初始化函數(shù),根據(jù)上面的兩種情況修正初始化的設(shè)置項(xiàng)。最后要恢復(fù)原來的bootstrapValidator函數(shù)(這一步是必須的,原來的bootstrapValidator函數(shù)有自己的一大堆關(guān)聯(lián)的東東,不能丟失);

實(shí)現(xiàn):

/*add chenhua 2015.10.16 重寫bootstrapValidator方法?給每一個(gè)驗(yàn)證項(xiàng)都添加禁止輸入"&"符號(hào)*/ 
$(function(){  //保存原始的bootstrapValidator 
  var overwrite = $.fn.bootstrapValidator;   //重載bootstrapValidator
  $.fn.bootstrapValidator = function(options){ 
    //恢復(fù)原來的bootstrapValidator,因?yàn)槠浼恿撕芏鄶?shù)據(jù)是不能丟失的 
    $.fn.bootstrapValidator = overwrite; 
            //這里有兩種做法,第一種是直接修改arguments內(nèi)容,使其包含不能輸入&的驗(yàn)證,然后調(diào)用即可;    //第二種是先使用arguments來初始化,然后使用調(diào)用bootstrapValidator的函數(shù)來給非正則表達(dá)式驗(yàn)證的項(xiàng)添加不能輸入&的驗(yàn)證    //這里我們使用了第二中?! ?
    var validtor = overwrite.apply(this,arguments); 
    if($.type(arguments[0]) == "object"){ 
      var vtor = this.data("bootstrapValidator"),      //過濾出輸入框表單項(xiàng)   
      fileds = this.find("input[name][type='hidden'],input[name][type='password'],input[name][type='text'],textarea[name]").not(":disabled,[type='hidden']"); 
      fileds.each(function(){ 
        //本身沒有正則驗(yàn)證才添加不能輸入&的驗(yàn)證 
        if(!vtor.getOptions($(this).attr('name'),'regexp','regexp')){ 
         vtor.addField($(this).attr('name'), 
            { 
              validators: { 
                regexp: { 
                  regexp: /^[^&]*$/, 
                  message: "不能包含&字符" 
                } 
              } 
          }) 
        } 
      }) 
    } 
    return validtor; 
  } 
}) 

以上內(nèi)容是腳本之家小編給大家介紹的基于jQuery 實(shí)現(xiàn)bootstrapValidator下的全局驗(yàn)證,希望大家喜歡。

相關(guān)文章

最新評(píng)論