0
我在我的頭上撞牆,所以我非常感謝這方面的幫助。我創建了一些驗證方法來指向一個javascript函數,如果電子郵件目前沒有/正在我們的會員表中,則返回true/false。 javascript函數(CheckEmail)效果很好,控制器操作正常,但無論CheckEmail是否返回true或false,它總是評估爲無效字段。請幫助:jquery自定義驗證(addMethod)返回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>
@user ....難道是關係到「返回this.optional(元素)」。嘗試刪除該代碼,看看會發生什麼 – MikeTWebb
我做過。沒有骰子:( – user576838