0

我在这个问题上撞墙了,所以我很感激这方面的任何帮助。我有一些我创建的验证方法指向一个 javascript 函数,如果电子邮件不是/当前在我们的成员表中,则返回 true/false。javascript 函数 (CheckEmail) 运行良好,控制器操作运行良好,但无论 CheckEmail 返回 true 还是 false,它总是评估为无效字段。请帮忙:

验证

 $.validator.addMethod("emailCheck", function (value, element) {
                return this.optional(element) || CheckEmail(value);
            }, "This email is already registered");

            Validator = $("#RegForm").validate({
                rules: {
                    Email: {
                        required: true,
                        email: true,
                        emailCheck: true
                    },
                    Password: {
                        required: true,
                        minlength: 6
                    },
                    ConfirmPassword: {
                        required: true,
                        equalTo: "#Password"
                    },
                    "UserObj.FirstName": {
                        required: true,
                        minlength: 2
                    },
                    "UserObj.LastName": {
                        required: true,
                        minlength: 2
                    }
                }

            });

javascript方法:

function CheckEmail(email) {
    $.getJSON("/account/jsonCheckEmail", { EmailToCheck: email }, function (data) {
        if (data.status = "OK") {
            if (data.msg = "True") {
                return true;
            }
            else {
                return false;
            }
        }
    });

}

控制器动作:

public JsonNetResult jsonCheckEmail(string EmailToCheck)
{
    JsonNetResult jsonEmail = new JsonNetResult();
    jsonEmail.Formatting = Formatting.Indented;
    try
    {

        string user = Membership.GetUserNameByEmail(EmailToCheck);

        if (string.IsNullOrWhiteSpace(user))
        {
            jsonEmail.Data = new jsonResponseObj("True", "True");
        }
        else {
            jsonEmail.Data = new jsonResponseObj("False","False");
        }

    }
    catch (Exception ex)
    {
        jsonEmail.Data = new jsonResponseObj(ex);
        //jsonEmail.Data = new jsonResponseObj("False", "False");
    }

    return jsonEmail;
}

HTML:

               <tr>
                    <td>@Html.LabelFor(m => m.Email)
                    </td>
                    <td>@Html.TextBoxFor(m => m.Email)
                        @Html.ValidationMessageFor(m => m.Email)
                    </td>
                </tr>
4

1 回答 1

1

您可以利用插件的远程工具,而不是编写自己的 jQuery 处理程序:

Email: {
     required: true,
     email: true,
     remote: "/account/jsonCheckEmail"
},
于 2012-09-27T03:42:47.243 回答