Home > DeveloperSection > Forums > Dropdown list textbox required field validator generate error
Royce Roy
Royce Roy

Total Post:134

Points:938
Posted on    October-27-2014 1:40 AM

 ASP.Net ASP.Net  Validation 
Ratings:


 1 Reply(s)
 637  View(s)
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>



Samuel Fernandes
Samuel Fernandes

Total Post:156

Points:1096
Posted on    October-27-2014 1:50 AM

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>


Don't want to miss updates? Please click the below button!

Follow MindStick