21

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;
            }
        }
    }
Kelsey
  • 47,246
  • 16
  • 124
  • 162
Mike
  • 713
  • 6
  • 20
  • 41

3 Answers3

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?

Community
  • 1
  • 1
Kelsey
  • 47,246
  • 16
  • 124
  • 162
  • doesn't work for me , `source.errormessage = "custom message here";` works fine – Mahmoud Farahat Jan 08 '13 at 09:55
  • @Mahmoud corrected posting. Typically if the answer is already in the comment you can submit an edit to the answer (especially when it is the accepted answer) to have it improved. – Kelsey Jan 08 '13 at 15:55
  • 4
    didn't work for me using ie9 but either of the following worked: source.innerHTML = "custom message here"; or $(source).html("custom message here"); – doiley Jan 24 '13 at 22:30
  • @doiley Thanks. It worked for me in both ie and chrome – Bat_Programmer Sep 06 '18 at 00:59
11

well source.errormessage not worked correctly some time

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

Jay Magwadiya
  • 410
  • 2
  • 6
  • 14
  • I would like to know why `innerText` works instead of `errormessage`. Is it to do with .net version? – umutesen Aug 01 '17 at 14:18
1
source.errormessage = "custom message here";
Mahmoud Farahat
  • 5,364
  • 4
  • 43
  • 59