下面是我的 Html 代码和 Javascript 代码。
<h2>Register</h2>
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
<fieldset class="acloginform">
<legend>Register User</legend>
<ol>
<li>
<div class="editor-label">
@Html.LabelFor(model => model.Email_Id)
</div>
<div class="editor-field">
@Html.TextBoxFor(model => model.Email_Id, new { @id = "txt_EmailId" })
@Html.ValidationMessageFor(model => model.Email_Id)
</div>
</li>
<li>
<div class="editor-label">
@Html.LabelFor(model => model.FirstName)
</div>
<div class="editor-field">
@Html.TextBoxFor(model => model.FirstName)
@Html.ValidationMessageFor(model => model.FirstName)
</div>
</li>
<li>
<div class="editor-label">
@Html.LabelFor(model => model.LastName)
</div>
<div class="editor-field">
@Html.TextBoxFor(model => model.LastName)
@Html.ValidationMessageFor(model => model.LastName)
</div>
</li>
<li>
<div class="editor-label">
@Html.LabelFor(model => model.Password)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Password)
@Html.ValidationMessageFor(model => model.Password)
</div>
</li>
<li>
<div class="editor-label">
@Html.LabelFor(model => model.ConfirmPassword)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.ConfirmPassword)
@Html.ValidationMessageFor(model => model.ConfirmPassword)
</div>
</li>
<li>
<input type="submit" value="Register" />
@Html.ActionLink("Back to Home Page", "Index", new { Controller = "Home" }, new { @class = "regislnk" })
</li>
</ol>
</fieldset>
}
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
<script type="text/javascript">
$(document).ready(function () {
jQuery.validator.addMethod("Duplicate_EmailId", function (value) {
$.ajax({
url: '/User/CheckEmailId?EmailId=' + value,
async: false,
success: function (data) {
alert(data);
if (data == true)
return false;
else
return true;
},
});
}, "Email Id already Exists");
$('#txt_EmailId').addClass('Duplicate_EmailId');
});
</script>
我在用户控制器下的 c# 方法是
public JsonResult CheckEmailId(string EmailId)
{
var res = my method;
return Json(res, JsonRequestBehavior.AllowGet);
}
这里错误消息显示在每个结果成功/失败上。并且方法正在调用
两次。关于文本框的模糊以及表单提交。