Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamic RequiredFieldValidator

Status
Not open for further replies.

acent

Technical User
Feb 17, 2006
247
US
Greetings y'all,

I'm looking to dynamically turn on/off a couple requiredfieldvalidator controls - the easiest way possible.
I have a dropdownlist and some textbox controls:
Code:
<li><label for="ddlRepeats">Repeating</label>
  <asp:DropDownList ID="ddlRepeats" runat="server">
    <asp:ListItem Value="1" Selected="True">Does Not Repeat</asp:ListItem>
    <asp:ListItem Value="2" Selected="False">Same Day of the Month</asp:ListItem>
    <asp:ListItem Value="3" Selected="False">Every ___ Days</asp:ListItem>
  </asp:DropdownList>
</li>
<li id="repeatfield1" style="display:none;"><label for="txtRepeatDay" id="lblRepeatDay">Day to Repeat On</label><asp:TextBox ID="txtRepeatDay" MaxLength="2" Size="5" runat="server" /></li>
<li id="repeatfield2" style="display:none;"><label for="txtRepeatNo">No. of Repeats</label><asp:TextBox ID="txtRepeatNo" MaxLength="3" Size="5" runat="server" /></li>
And the DDL fires fires some javascript to show/hide textbox fields:
Code:
<script language="javascript">
  function toggle_repeat_fields(fld) {
    if(fld.value == 1) {
      document.getElementById('repeatfield1').style.display = 'none';
      document.getElementById('repeatfield2').style.display = 'none';
    } else {
      document.getElementById('repeatfield1').style.display = 'block';
      document.getElementById('repeatfield2').style.display = 'block';
    }
    if(fld.value == 2) {
      document.getElementById('lblRepeatDay').innerHTML = "Day to Repeat On";
    } else {
      document.getElementById('lblRepeatDay').innerHTML = "Day Interval";
    }
  }
</script>
Not exactly radical new therapy in web design. What I want is to add a requiredfieldvalidator to the two textboxes if they're visible.

Thought about a custom field validator, but that might be overcomplicating the obvious.

Any help is appreciated. I am using ASP/VB 2.0

"If it's stupid but works, it isn't stupid."
-Murphy's Military Laws
 
I haven't used validation webcontrols for a while, but i thought the validator was "disabled" if the visibility property is set to false. if this is correct
Code:
bool display = ShouldDisplayTextBox();
MyTextBox.Visible = display;
MyTextBoxRequiredFieldValidator.Visible = display;

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top