Jquery 如何配置触发哪个事件自定义验证方法

Jquery 如何配置触发哪个事件自定义验证方法,jquery,jquery-validate,Jquery,Jquery Validate,我正在使用jQuery验证插件 我有一个自定义的验证器方法。它查看电子邮件地址是否正在使用 $.validator.methods.notinuse = function (value, element, param) { $.post("/static/global/member/validation.ashx", { email: value}) .done(function (data) { return data == "true"; }); }

我正在使用jQuery验证插件

我有一个自定义的验证器方法。它查看电子邮件地址是否正在使用

$.validator.methods.notinuse = function (value, element, param) {

    $.post("/static/global/member/validation.ashx", { email: value})
    .done(function (data) {
        return data == "true";
    });
};
这就是所谓的那样

$("#form-Registration").validate({
    rules: {
        Email: {
            notinuse: ''
        }
    },
    messages: {
        Email: {
            notinuse: "Email address in use"

        }
    }
});
然而,这是可行的,它似乎会在每一个过于频繁的按键上触发

我的问题:


1) 使用自定义验证器方法,是否有方法配置调用它的事件?我认为无论是在表单提交上还是在文本字段上,关闭焦点都会起到作用。我希望它与js查询api保持一致。

正确的格式是将自定义方法/规则设置为
true
或传递参数,而不是将其设置为
'
。您还将
消息中的
notinuse
字段名拼写错误为
inuse

您可以将
onkeyup
选项设置为false,以避免在每次按键时触发它。这将限制您的方法仅在焦点退出和提交单击时触发。但是,此设置将影响窗体上的所有字段

$("#form-Registration").validate({
    onkeyup: false, // <-- disable "onkeyup" for all form fields
    rules: {
        Email: {
            notinuse: true
        }
    },
    messages: {
        Email: {
            notinuse: "Email address in use"

        }
    }
});
演示1(一个字段禁用onkeyup的两个字段):

演示2(在单个字段中禁用onkeyup的代码)

ps,修复了我示例中的“notinuse”错误。@frosty,不客气。实际上,这两个演示展示了完全相同的方法。第二个演示还显示了您的自定义方法。
$("#form-Registration").validate({
    onkeyup: function( element, event ) {
        if (element.name === "notinuse") {
            return false;
        } else if ( event.which === 9 && this.elementValue(element) === "" ) {
            return;
        } else if ( element.name in this.submitted || element === this.lastElement ) {
            this.element(element);
        }
    },
    rules: {
        Email: {
            notinuse: true
        }
    },
    messages: {
        Email: {
            notinuse: "Email address in use"

        }
    }
});