【问题标题】:Showing Validation error for server side validation显示服务器端验证的验证错误
【发布时间】:2012-01-18 22:26:52
【问题描述】:

我有一个 ajax 表单,在里面我有一个模型的编辑器,该模型具有带有自定义验证属性的电子邮件字段,用于检查服务器中是否已经存在电子邮件,所以它是这样的:

$("#submitPop").live("click", (function (e) {
       var form = $("#popForm");
        form.validate();
        if (form.valid()) {
            $.ajax({
                type: "POST",
                url: "/Account/RegisterEmail/",
                data: form.serialize(),
                dataType: "json",
                success: function (result) {
                    if (result.Status) {
                        location.reload(true);
                    } else {
                        alert("Something Went Wrong!"); //what should i write here to show the error message in its generated field-validation-error span
                    }
                }
            });
        }
        return false;

    }));

在 ajax 表单内:

       @Html.EditorFor(x => x.Email)
       @Html.ValidationMessageFor(x => x.Email)</li>

正如评论所说,当 ajax 结果返回 false 以在生成的 field-validation-error span 中显示错误消息时,我该怎么办?

我正在考虑一种本机方式来通知 jquery 错误并通知 jquery 更改所有需要的更改,例如放置红色 X 并使错误跨越字体红色等 类似:

$.Validator.ShowErrorMessageFor("Email","Email is Already Taken")

【问题讨论】:

    标签: c# jquery asp.net-mvc


    【解决方案1】:

    如果您使用的是 ASP.net MVC3 将建议使用 远程验证 属性

    错误消息将在字段验证错误消息跨度中呈现。

    ASP.NET MVC 3 provides a mechanism that can make a remote server call in order to validate a form field without posting the entire form to the server. This is useful when you have a field that cannot be validated on the client and is therefore likely to fail validation when the form is submitted. For example, many Web sites require you to register using a unique user ID. For popular sites, it can take several attempts to find a user ID that is not already taken, and the user's input is not considered valid until all fields are valid, including the user ID. Being able to validate remotely saves the user from having to submit the form several times before finding an available ID.

    查看此SO question remote-validation-in-asp-net-mvc-3- 或参考MSDN Article

    【讨论】:

    • 谢谢你,它有效!,这很好,但我只有一个怀疑,每次文本框更改时都会回发,如果它只在文本框失去焦点时发布会更好,你不觉得?
    • Stacker 我认为这是远程验证的默认行为。很想看看我们是否可以定制它
    【解决方案2】:

    我假设span的Id是field-validation-error,那么应该是这样的,

    $('#field-validation-error').text('Email already exist, Please enter a different email ID');
    

    【讨论】:

    • 没有跨度没有id它只有一个“字段验证错误”类,当然还有其他跨度也有这个
    • 我在想一些更本土的东西
    • @Stacker:你想在哪里显示验证错误?该跨度与具有相同班级的另一个跨度有什么不同吗?如果是,那么我们可以使用它在该范围内设置错误消息。如果你有ID,那么我们可以使用简单的原生js来设置innerHTML。
    【解决方案3】:

    首先,给 span 设置一个 id:

    <span class="field-validation-error" id="validEmail"></span>
    

    那么你可以使用:

    $('#validEmail').text('Email already exist...');
    

    【讨论】:

    • 正如您在我更新的问题中看到的那样,我不控制其生成的跨度的 id。
    • 为验证范围设置一个 id 简单使用:@Html.ValidationMessageFor(x =&gt; x.Email, "Please enter a valid email", new { id = "validEmail" })
    • 我试过这个,但即使我这样做了,jquery 会在 span 中添加很多东西,使它看起来像一条错误消息,比如在它旁边添加 Red X 并将字体设置为红色等,我正在考虑一种将错误通知 jquery 的本地方式。
    猜你喜欢
    • 1970-01-01
    • 2017-03-03
    • 2019-09-08
    • 2015-07-08
    • 2014-06-21
    • 2017-08-18
    • 1970-01-01
    • 2014-06-29
    • 1970-01-01
    相关资源
    最近更新 更多