Dynamic error message for custom validator clientside
Asked Answered
J

3

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;
            }
        }
    }
Jog answered 22/3, 2011 at 16:51 Comment(0)
T
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?

Tricuspid answered 22/3, 2011 at 17:2 Comment(4)
doesn't work for me , source.errormessage = "custom message here"; works fineDanielldaniella
@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.Tricuspid
didn't work for me using ie9 but either of the following worked: source.innerHTML = "custom message here"; or $(source).html("custom message here");Garrity
@Garrity Thanks. It worked for me in both ie and chromeRepartee
S
11

well source.errormessage not worked correctly some time

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

Sharpedged answered 22/8, 2014 at 6:31 Comment(1)
I would like to know why innerText works instead of errormessage. Is it to do with .net version?Gahan
D
1
source.errormessage = "custom message here";
Danielldaniella answered 8/1, 2013 at 12:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.