2015-11-23 137 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.' 
    } 
    });