Imad
Imad

Reputation: 7490

ValidatorEnable not working

My code is as below

<asp:DropDownList ID="ddlFilter" runat="server" onchange="ShowHideSearchTextBox(this)">
                                <asp:ListItem Value="0">--Select--</asp:ListItem>
                                <asp:ListItem Value="customer_name">Customer Name</asp:ListItem>
                                <asp:ListItem Value="order_number">Order Number</asp:ListItem>
                                <asp:ListItem Value="Pending">Dispatch Pending</asp:ListItem>
                                <asp:ListItem Value="Done">Dispatch Done</asp:ListItem>
                            </asp:DropDownList>

<script type="text/javascript">
        function ShowHideSearchTextBox(me) {
            var searchBox = document.getElementById("searchBox");
            var validator = document.getElementById(" <%= RequiredFieldValidator1.ClientID%> ");
            if (me.value == "Pending" || me.value == "Done") {
                ValidatorEnable(validator, false);
                searchBox.style.display = "none";
            }
            else {
                ValidatorEnable(validator, true);
                searchBox.style.display = "block";
            }
        }
    </script>

Regardless of what value I select in drop down, my validator is always active. What am I misssing?

EDIT

value of validator is coming null

Upvotes: 2

Views: 7105

Answers (1)

Kelsey
Kelsey

Reputation: 47726

Hi I assume your Validator is implemented correctly so they problem is with how you are accessing the validator in the javascript:

var validator = document.getElementById(" <%= RequiredFieldValidator1.ClientID%> ");

Change it to:

var validator = document.getElementById("<%= RequiredFieldValidator1.ClientID %>");

Removing the extra white space within the quotes should solve the problem as getElementById does not remove the white space for you.

Tested your code above (with a valid validator) and the change fixed the problem.

Upvotes: 1

Related Questions