ConditionalRequiredTextValidator Control
Description
The ConditionalRequiredTextValidator control allows you to specify a CheckBox, RadioButton, CheckBoxList, RadioButtonList, DropDownList, or ListBox that determines whether a TextBox should be required. This can be useful when designing a form where a piece of information is only necessary under certain conditions. Like all true validators, the ConditionalRequiredTextValidator inherits from System.Web.UI.WebControls.BaseValidator, so I will not list those properties here.
Example
 ft

 ft





 
 ft
 



CheckBox

RadioButton

CheckBoxList


 
 ft
 


 ft



 ft



RadioButtonList

DropDownList

ListBox
The following tags were used to create the validators used in the six examples above:

Properties & Methods
ConditionalRequiredTextValidatorThe ConditionalRequiredTextValidator validates a specified TextBox based on the status of a CheckBox, RadioButton, CheckBoxList, RadioButtonList, DropDownList, or ListBox.
  • ControlToValidate - (Inherited from BaseValidator) The ID of the TextBox that may be required
  • ConditionControlID - The ID of the control that determines whether the Textbox should be required
  • ConditionalType - A value from the ConditionControlType enumeration that specifies the type of control specified in ConditionControlID
  • ConditionalItemIndex - Specifies the index of the ListItem that determines whether the TextBox should be validated when using one of the List controls
  • ConditionalItemText - Specifies the Text property of the ListItem that determines whether the TextBox should be validated when using one of the List controls. If multiple ListItems have this value for their Text property, the first one will be used. If a ListItem with this value for their Text property does not exist, an error may occur
  • ConditionalItemValue - Specifies the Value property of the ListItem that determines whether the TextBox should be validated when using one of the List controls If multiple ListItems have this value for their Value property, the first one will be used. If a ListItem with this value for their Value property does not exist, an error may occur
  • UseReverseValidation - Specifies whether to require validation when ConditionControlID is selected or not selected. When set to False (the default), validation occurs only when ConditionControlID is checked/selected, when set to True validation occurs only when ConditionControlID is not checked/selected
ConditionControlType As ByteAn enumeration used for specifying the control type that determines whether the TextBox is required.
  • CheckBox
  • RadioButton
  • CheckBoxList
  • RadioButtonList
  • DropDownList
  • ListBox
Source Code
ConditionalRequiredTextValidator.vb:



ConditionalRequiredTextValidator.js:

Remarks
When using one of the List types for the ConditionalType property, be sure to specify a ConditionalItemIndex, ConditionalItemText, or ConditionalItemValue. You may also want to add JavaScript to hide or disable the TextBox when using the DropDownList or ListBox to avoid confusing users. When setting the UseReverseValidation property to True, keep in mind that the ConditionalRequiredTextValidator determines whether the TextBox needs validated based only on the single specified item of the ConditionControlID. If you want validation to occur when another item in the list is selected, be sure to include a RequiredFieldValidator to make sure something is selected.