【问题标题】:Dynamic error message for custom validator clientside自定义验证器客户端的动态错误消息
【发布时间】:2011-03-22 16:51:29
【问题描述】:

我正在使用自定义验证器来调用 javascript 函数进行验证。我的问题是我需要能够动态更改错误消息。代码如下:

            <asp:CustomValidator ID="CustomValidator1" runat="server" ClientValidationFunction="fcnValid1"
                ErrorMessage=""  Display="None" ValidateEmptyText="True">
            </asp:CustomValidator>

<asp:ValidationSummary ID="ValidationSummary1" runat="server" DisplayMode="List" ShowMessageBox="True" ShowSummary="False" />

    function fcnValid(source, args) {
        var Status = document.getElementById("<%=ddlStatus.ClientID%>").value

        if (Status == "In Underwriting") {
            if (document.getElementById("<%=txtRequestor.ClientID%>").value == "") {
                //sender.errormessage = "Test1"
                //sender.innerHTML = "Test2";
                document.getElementById("<%=txtRequestor.ClientID%>").focus();
                args.IsValid = false;
            }
        }
    }

【问题讨论】:

    标签: javascript asp.net ajax customvalidator


    【解决方案1】:

    在您的验证 javascript 中,您可以通过 source 访问它来更改消息:

    source.errormessage = "custom message here";
    

    在 SO 上找到了这个问题,它也应该为您提供更多信息:

    How can I rewrite the ErrorMessage for a CustomValidator control on the client?

    【讨论】:

    • 对我不起作用,source.errormessage = "custom message here"; 工作正常
    • @Mahmoud 更正了帖子。通常,如果答案已经在评论中,您可以提交对答案的编辑(尤其是当它是已接受的答案时)以对其进行改进。
    • 使用 ie9 对我不起作用,但以下任一方法起作用:source.innerHTML = "custom message here";或 $(source).html("此处自定义消息");
    • @doiley 谢谢。它在 ie 和 chrome 中都适用于我
    【解决方案2】:

    好吧source.errormessage有些时候不能正常工作

    我建议使用 source.innerText="error message";

    【讨论】:

    • 我想知道为什么innerText 工作而不是errormessage。和.net版本有关吗?
    【解决方案3】:
    source.errormessage = "custom message here";
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-11-08
      • 2012-01-21
      • 2017-12-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-12
      相关资源
      最近更新 更多