我在这个上面撞墙,所以我很感激你的帮助。我有一些我创建的验证方法指向一个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>
答案 0 :(得分:1)
您可以利用插件的remote工具,而不是编写自己的jQuery处理程序:
Email: {
required: true,
email: true,
remote: "/account/jsonCheckEmail"
},