• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 766
  • Last Modified:

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.
0
jsuanque
Asked:
jsuanque
2 Solutions
 
David H.H.LeeCommented:
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
 
ajitha75Commented:
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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now