2015-11-23 123 views
0

我有一個自定義的驗證,如下圖所示...parsley.js:問題與自定義的驗證

window.Parsley 
    .addValidator('invalidwords', { 
    requirementType: 'regexp', 
    validateString: function(value, requirement) { 
     var wordval = value.split(" "); 
     $.each(wordval,function(idx,item) { 
      return !/^\b(?:Stadium|GT|BB|HB|Simul|VNOSE|LT|combination|LT1|SSGT|BW|HBS|simul|combo|2hbs|4d|lt2|theatre)\b$/i.test(item) 
     }); 
    }, 
    messages: { 
     en: 'Invalid words detected.' 
    } 
    }); 

基本上我想要的是檢查一個字符串是否包含在我的正則表達式的任何話。 在我添加each()函數之前,它會適用於單個單詞,但當我輸入gt lt之類的東西時,它不起作用,所以我必須將它們放入數組中並檢查每個單詞。 它似乎工作時,我調試,因爲它確實返回false,但它似乎好像香菜沒有看到它或什麼的效果。

這是我如何打電話吧...

<div class="col-sm-6 col-lg-6"> 
    <div class="form-group"> 
     <input type="text" name="company" data-parsley-group="eventinfo" id="Company" class="form-control" placeholder="Company" maxlength="60" tabindex="3" title="Company" parsley-trigger="keyup" data-parsley-invalidwords="" value="#request.args.company#"> 
    </div> 
</div> 

我也試圖改變requirementType: 'regexp',requirementType: 'string',

回答

0

想通弄明白了。必須做循環外的回報。這是我的最終代碼。

window.Parsley 
    .addValidator('invalidwords', { 
    requirementType: 'regexp', 
    validateString: function(value, requirement) { 
     var wordval = value.split(" "); 
     var valid = true; 
     $.each(wordval,function(idx,item) { 
      console.log(item,/^\b(?:Stadium|GT|BB|HB|Simul|VNOSE|LT|combination|LT1|SSGT|BW|HBS|simul|combo|2hbs|4d|lt2|theatre)\b$/i.test(item)); 
      if(/^\b(?:Stadium|GT|BB|HB|Simul|VNOSE|LT|combination|LT1|SSGT|BW|HBS|simul|combo|2hbs|4d|lt2|theatre)\b$/i.test($.trim(item))){ 
       valid = false; 
      } 
     }); 
     return valid; 
    }, 
    messages: { 
     en: 'Invalid words detected.' 
    } 
    });