最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Dynamic error message for custom validator clientside - Stack Overflow

programmeradmin1浏览0评论

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:

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

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:

            <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;
            }
        }
    }
Share Improve this question edited Mar 22, 2011 at 17:09 Kelsey 47.7k16 gold badges127 silver badges162 bronze badges asked Mar 22, 2011 at 16:51 MikeMike 7436 gold badges20 silver badges45 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 21

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

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?

well source.errormessage not worked correctly some time

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

source.errormessage = "custom message here";
发布评论

评论列表(0)

  1. 暂无评论