DROPDOWN LIST TEXTBOX REQUIRED FIELD VALIDATOR GENERATE ERROR

Royce Roy

Total Post:149

Points:1043
Posted by  Royce Roy
 950  View(s)
Ratings:
Rate this:

I have a dropdown list for the which values are coming from DB. There is one option name OTHER, when I select other the textbox appears. For that Textbox I have made a required field validator. Till here it works fine. But when I select any other option, still it gives me validator errors. It should not happen. It should only give me required field error when the user doesn't fills the textbox on select of OTHER option. Please see the code.

Dropdown and Textbox code:-

<asp:DropDownList ID="ddlGraduation" runat="server" CssClass="txtfld-popup_drp"></asp:DropDownList>

 <asp:RequiredFieldValidator CssClass="error_msg" ID="reqGraduation" runat="server" ControlToValidate="ddlGraduation" ErrorMessage="Please select graduation details" InitialValue="--Select--" SetFocusOnError="true"></asp:RequiredFieldValidator>

<asp:TextBox ID="txtOther" runat="server" CssClass="txtfld-popup_p"></asp:TextBox>                                                                               

<asp:RequiredFieldValidator ID="reqOther" runat="server" ControlToValidate="txtOther" ErrorMessage="Please specify your qualification"></asp:RequiredFieldValidator>

JS code for hiding and show the textbox when user select and deselect the OTHER option from dropdown list:-


<script type="text/javascript">

    function pageLoad() {

        $('#ctl00_ContentPlaceHolder1_txtOther').hide();

        $('#ctl00_ContentPlaceHolder1_ddlGraduation').change(function () {

            if ($(this).val() === "Other") {

                $('#ctl00_ContentPlaceHolder1_txtOther').show();

            } else {

                $('#ctl00_ContentPlaceHolder1_txtOther').hide();

            }

        });

    };

</script>

  1. Samuel Fernandes

    Post:159

    Points:1117
    Re: Dropdown list textbox required field validator generate error

    You need to enable your required field validator only when your textbox is visible, you need to write below line of code in your javascriot function when you are showing your textbox:


     ValidatorEnable(document.getElementById("reqGraduation"), true);

    Your javascript code shuld be like this:

    <script type="text/javascript">

        function pageLoad() {

            $('#ctl00_ContentPlaceHolder1_txtOther').hide();

            $('#ctl00_ContentPlaceHolder1_ddlGraduation').change(function () {

                if ($(this).val() === "Other") {

                    $('#ctl00_ContentPlaceHolder1_txtOther').show();

     

                    ValidatorEnable(document.getElementById("reqGraduation"), true);

                } else {

                    $('#ctl00_ContentPlaceHolder1_txtOther').hide();

     

                    ValidatorEnable(document.getElementById("reqGraduation"), false);

                }

            });

        };

    </script>

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!