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

时间:2014-03-27 18:48:52

标签: javascript jquery jquery-plugins jquery-validate

我遇到了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之外。 Formatter是否擦除了jquery验证插件所依赖的一些事件处理程序,因此,我需要为其添加其他代码?任何见解/代码样本都将受到赞赏。

1 个答案:

答案 0 :(得分:0)

问题来自于formatter.js格式化程序将格式化输出放在文本框的值中,即使用户没有为字段提供值也是如此。因此,在我的示例中,除非我将persistent设置为false,否则提供的模式将无法使用jquery插件中的必需字段验证:

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