22

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

3 Answers 3

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?

Sign up to request clarification or add additional context in comments.

4 Comments

doesn't work for me , source.errormessage = "custom message here"; works fine
@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.
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 Thanks. It worked for me in both ie and chrome
11

well source.errormessage not worked correctly some time

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

1 Comment

I would like to know why innerText works instead of errormessage. Is it to do with .net version?
1
source.errormessage = "custom message here";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.