How to make a dynamic Required field validator

Hello experts,
I wonder if it's possible to create a dynamic required field validator. The logic i'm after for is this...
IF Textbox1 is  null or ='' THEN TEXTBOX2 is required (i mean user needs to supply data) ELSE Textbox2 is not required.

Is this possible? Any code suggestion/s or links is much appreciated.
jsuanqueAsked:
Who is Participating?
 
David H.H.LeeConnect With a Mentor Commented:
Hi jsuanque,
You can group it into different validation group instead of creating dynamic required field validator.
eg:
<asp:requiredfieldvalidator id="RequiredFieldValidator1"
      controltovalidate="NameTextBox"
      validationgroup="PersonalInfoGroup"
      errormessage="Enter your name."
      runat="Server">
    </asp:requiredfieldvalidator>

    <br /><br />

    <asp:label id="AgeLabel"
      text="Enter your age:"
      runat="Server"
      AssociatedControlID="AgeTextbox">
    </asp:label>

<!-- Different validation group -->
 <asp:label id="CityLabel"
      text="Enter your city of residence:"
      runat="Server"
       AssociatedControlID="CityTextbox">
    </asp:label>

    &nbsp;

    <asp:textbox id="CityTextbox"
      runat="Server">
    </asp:textbox>

    &nbsp;

    <asp:requiredfieldvalidator id="RequiredFieldValidator3"
      controltovalidate="CityTextBox"
      validationgroup="LocationInfoGroup"
      errormessage="Enter a city name."
      runat="Server">
    </asp:requiredfieldvalidator>

Check this MSDN site for further details:
http://msdn.microsoft.com/en-us/library/ms227424.aspx
0
 
ajitha75Connect With a Mentor Commented:
You can write a client side validation like this..

    <script language="javascript" type="text/javascript">
        function EnableValidator() {
            if (document.getElementById("TextBox1").value == "")
                document.getElementById("RequiredFieldValidator2").enabled = true;
               
        }
    </script>

        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:RequiredFieldValidator ControlToValidate="TextBox1"  ID="RequiredFieldValidator1" runat="server" ErrorMessage="RequiredFieldValidator"></asp:RequiredFieldValidator>
        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
        <asp:RequiredFieldValidator ID="RequiredFieldValidator2" ControlToValidate="TextBox2" Enabled="false" runat="server" ErrorMessage="RequiredFieldValidator"></asp:RequiredFieldValidator>
        <asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="EnableValidator()" OnClick="Button1_Click"  />

Thanks
Ajitha
0
 
jsuanqueAuthor Commented:
Nice Thanks. Having two different approaches certainly put things a little bit more interesting. Anyway...took me some time to replicate suggestion/s but it's worth it.  Thanks again.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.