jQuery Validator addMethod - 始终生成错误

时间:2016-12-27 19:13:18

标签: jquery jquery-validate

在以下代码中,无论checkEMailExists是返回true还是false,都会生成错误消息。我究竟做错了什么? ajax来电正常。

$().ready(function () {

$.validator.addMethod("checkEmailExists",
        function (value, element) {
            $.when(checkEmail()).done(function (response) {
                console.log((response == 1 ? "true" : "false"));
                return response == 1; // returns true if response is 1, false otherwise
            });
        },
        "This email address is registered"
        );

$("#loginform").validate({
    rules: {
        email: {
            required: true,
            checkEmailExists: true
        }
    },
    messages: {
        email: {
            required: "Please enter a valid email address",
            checkEmailExists: "This email address is already registered"
        }}
});

function checkEmail() {
    var email = $("#email").val();
    var data = {email: email};
    return $.ajax({
        type: "POST",
        url: "http://localhost/check_email/check_email.php",
        data: data
    });
}

0 个答案:

没有答案