【问题标题】:parsley.js: issue with custom validatorparsley.js:自定义验证器的问题
【发布时间】:2015-11-23 22:05:01
【问题描述】:

我有一个自定义验证器,如下所示...

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,但似乎 parsley 没有看到它或类似的东西。

我是这样称呼它的...

<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',

【问题讨论】:

    标签: parsley.js


    【解决方案1】:

    想通了。必须在循环外返回。这是我的最终代码。

    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.'
        }
      });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多