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

Incompatibility with formatter.js and jquery validation plugin?

本文关键字:验证 插件 不兼容 jquery formatter js      更新时间:2023-09-26

我遇到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
    });
}

如果我注释掉那块代码,该字段的要求被强制执行…但是,当两者都到位时,不会发生对Phone字段的验证。似乎这两款游戏并不是为了兼容而设计的。

具体来说,在调用checkForm()之后,errorMap对象拥有除了Phone之外的所有验证失败。Formatter是否清除了jquery验证插件所依赖的一些事件处理程序,因此,我需要添加额外的代码?

这个问题来自于Formatter .js中的Formatter将把格式化输出放在文本框的值中,即使用户没有向该字段提供值。因此,在我的示例中,除非我将persistent设置为false,否则提供的模式将无法处理来自jquery插件的所需字段验证:

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