-1

下面是我的 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);
}

这里错误消息显示在每个结果成功/失败上。并且方法正在调用

两次。关于文本框的模糊以及表单提交。

4

1 回答 1

0

你的直接问题是你没有从你的验证方法中返回任何东西,所以这就像返回 false

jQuery.validator.addMethod("Duplicate_EmailId", function (value) {

    // returning nothing here, means validation fails

}, "Email Id already Exists");

您从 ajax 方法返回 true/false,但不是从验证方法返回,您需要这样的东西

jQuery.validator.addMethod("Duplicate_EmailId", function (value) {

    var validationResult = false;

    $.ajax({
        url: '/test/CheckEmailId?EmailId=' + value,
        async: false,
        success: function (data) {
            console.log(data);
            if (data == true)
                validationResult = false;
            else
                validationResult = true;
        },
    });

    return validationResult;

}, "Email Id already Exists");

我会说像这样将 async 设置为 false 并不理想,因为它会在进行 ajax 调用时禁止用户填写其他字段。远程方法的实现中有一些聪明的东西,所以它可以是异步的。如果可以使用,请使用插件的远程方法。

于 2013-04-09T10:48:10.517 回答