代码之家  ›  专栏  ›  技术社区  ›  Jason

与formatter.js和jquery验证插件不兼容?

  •  -1
  • Jason  · 技术社区  · 11 年前

    我遇到了formatter.js和验证jquery插件的问题。

    <div class="formClear">
        <label for="text-basic">
            Phone<em> *</em></label>
        <input type="tel" name="Phone" id="Phone" class="required" />
    </div>
    

    当我将以下格式化程序应用于特定字段,然后使用jquery验证插件将其设置为必填字段时,jquery插件不再将其作为必填字段强制执行。

    if (document.getElementById('Phone') != null) {
        new Formatter(document.getElementById('Phone'), {
            'pattern': '({{999}}) {{999}}-{{9999}}',
            'persistent': true
        });
    }
    

    如果我把这段代码注释掉,字段的必填项就会强制执行。。。但当两者都到位时,“电话”字段的验证不会发生。就好像这两者并不是为了兼容而设计的。

    具体来说,在调用checkForm()之后,errorMap对象具有除Phone之外的所有验证失败。格式化程序是否删除了jquery验证插件所依赖的一些事件处理程序,因此,我需要添加额外的代码?如有任何见解/代码示例,将不胜感激。

    1 回复  |  直到 11 年前
        1
  •  0
  •   Jason    11 年前

    问题来自这样一个事实:即使用户没有向字段提供值,Formatter.js中的Formatter也会将格式化输出放入文本框的值中。因此,在我的示例中,除非我将persistent设置为false,否则所提供的模式将永远无法与jquery插件所需的字段验证一起工作:

    if (document.getElementById('Phone') != null) {
        new Formatter(document.getElementById('Phone'), {
            'pattern': '({{999}}) {{999}}-{{9999}}',
            'persistent': false
        });
    }