自定义验证程序客户端动态错误消息

时间:2022-10-07 16:27:13

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;
            }
        }
    }

3 个解决方案

#1


16  

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

在您的验证javascript中,您可以通过源代码访问消息:

source.errormessage = "custom message here";

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

找到了这个问题,以便给你更多的信息:

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

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

#2


9  

well source.errormessage not worked correctly some time

源。错误信息在一段时间内不能正常工作。

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

我的建议是使用源代码。innerText =实现“错误信息”;

#3


1  

source.errormessage = "custom message here";

#1


16  

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

在您的验证javascript中,您可以通过源代码访问消息:

source.errormessage = "custom message here";

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

找到了这个问题,以便给你更多的信息:

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

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

#2


9  

well source.errormessage not worked correctly some time

源。错误信息在一段时间内不能正常工作。

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

我的建议是使用源代码。innerText =实现“错误信息”;

#3


1  

source.errormessage = "custom message here";