Conditional Web Page required field validator

Posted on 2012-08-10
Last Modified: 2012-08-13
I have a web I inherited

There are two items which I need to link together

First...I can easily add the required field validator for either control but have it for the dropdown list only right now

What I need is that IF dropdown list selected value = "Yes"
The txtTravelCo_ is required to be filled in

<asp:DropDownList ID="ddlTravelAssign_" runat="server" CssClass="requiredDDL">
  <asp:ListItem Text="-- Select One --" Value=""></asp:ListItem>
  <asp:ListItem Text="Yes" Value="Yes"></asp:ListItem>
  <asp:ListItem Text="No" Value="No"></asp:ListItem>

<asp:TextBox ID="txtTravelCo_" runat="server" ></asp:TextBox>
Question by:lrbrister
    LVL 25

    Expert Comment

    by:Kyle Hamilton
    post your current script, then we can add to it.
    LVL 13

    Accepted Solution

    you can do that by using a custom validator what is a little javascript that is executed before a postback is executed - like all client validation methods (like required etc.)

    Here is a sample that is pretty much straight forward.
        <form id="form1" runat="server">
            <asp:DropDownList ID="ddlTravelAssign_" runat="server" >
            <asp:ListItem Selected="True" Text="--SELECT ONE--" Value=""></asp:ListItem>
            <asp:ListItem Text="YES" Value="Yes"></asp:ListItem>
            <asp:ListItem Text="NO" Value="No"></asp:ListItem>
            <br /><asp:TextBox ID="txtTravelCo_" runat="server" ></asp:TextBox> 
            <asp:CustomValidator id="MyValidator" ClientValidationFunction="MyValidatorFunction" runat="server" ErrorMessage="Please type in a text."></asp:CustomValidator>
            <br /><asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
        <script type="text/javascript">
            function MyValidatorFunction(Source, args) {
                var condidtionControlSource = document.getElementById("<%=ddlTravelAssign_.ClientID%>");
                var controlToValidate = document.getElementById("<%=txtTravelCo_.ClientID%>");
                if (condidtionControlSource.value == "Yes") {
                    args.IsValid = (controlToValidate.value.length > 0);
                args.IsValid = true;

    Open in new window

    Hope that helps :)

    Author Closing Comment

    LVL 13

    Expert Comment

    You are welcome :)

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Showing your events from Google Calendar in Google Maps Why? I travel all week and I thought it would be ideal if staff in office knew where I was based on my calendar. (OK real reason: my son wanted to see where I would be working, and I thoug…
    Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
    In this tutorial viewers will learn how to style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …
    The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

    758 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now