Click here to Skip to main content
15,892,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have one form in which i have one dropdown and one textbox. This textbox is depends on dropdown selection. when dropdown value is 'Other' then the textbox is enabled otherwise it is disabled. I want to apply javascript validation to the textbox, when textbox is enabled then it should not be emply while submiting the form.
JavaScript
function sugvalidate() {
     var related = document.getElementById('<%=DropDownList1.ClientID %>').value;
     var cat = document.getElementById('<%=txt_category.ClientID %>').value;
            if (related == "Other") {
                if (cat == "") {
                    alert("Please specify Category");
                    return false;
                }
            }
         }

ASP.NET
<asp:DropDownList ID="DropDownList1" runat="server" CssClass="ddcss" AutoPostBack="True" onselectedindexchanged="DropDownList1_SelectedIndexChanged">
<asp:ListItem Text="Error/Bug/Issue/Problem" Value="9"></asp:ListItem>
<asp:ListItem Text="Other" Value="10"></asp:ListItem>
</asp:DropDownList>

<asp:TextBox ID="txt_category" runat="server" Enabled="False" ReadOnly="True"></asp:TextBox>

<asp:Button ID="btn_submit" runat="server" Text="Submit" OnClientClick="return sugvalidate();" onclick="btn_submit_Click"   />

C#
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (DropDownList1.SelectedItem.Text == "Other")
        {
            txt_category.ReadOnly = false;
            txt_category.Enabled = true;
        }
        else
        {
            txt_category.ReadOnly = true;
            txt_category.Enabled = false;
        }
    }
Posted
Updated 20-Jul-14 10:41am
v2

C#
function sugvalidate() {
     var related = document.getElementById('<%=DropDownList1.ClientID %>').value;
     var cat = document.getElementById('<%=txt_category.ClientID %>').value;
            if (related == "10") {
                if (cat == "") {
                    alert("Please specify Category");
                    return false;
                }
            }
         }
 
Share this answer
 
Hi Raj, I have done some modifications to your javascript code

C#
function sugvalidate() {
            var related = document.getElementById("<%= DropDownList1.ClientID %>");
            var selectedText = related.options[related.selectedIndex].text;

            var cat = document.getElementById("<%= txt_category.ClientID %>").value;

            if (selectedText == "Other") {
                if (cat === "") {
                    alert("Please specify Category");
                    return false;
                }
            }
        }


Hope this will solve your problem. :-)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900