Javascript 自定义验证器客户端的动态错误消息

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5394861/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 17:00:04  来源:igfitidea点击:

Dynamic error message for custom validator clientside

javascriptasp.netajaxcustomvalidator

提问by Mike

I am using a custom validator to call a javascript function for validation. My problem is that I need to be able to change the error message dynamically. Here is the code:

我正在使用自定义验证器调用 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;
            }
        }
    }

采纳答案by Kelsey

In your validation javascript you can change the message by accessing it via the source:

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

source.errormessage = "custom message here";

Found this question on SO that should give you some more information as well:

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

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

如何为客户端上的 CustomValidator 控件重写 ErrorMessage?

回答by Jay Magwadiya

well source.errormessagenot worked correctly some time

source.errormessage有一段时间没有正常工作

what i suggest is to use source.innerText="error message";

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

回答by Mahmoud Farahat

source.errormessage = "custom message here";