Home     About PeterBlum.com     Policies     Download a Licensed Product     Newsletter
Peter's Professional Validation
Enabling Validators Automatically

Validators are often associated with fields that are not in a state that they need to be validated. For example, unless a checkbox is marked, the textbox to its right is ignored. Microsoft's Validation Framework offers the Enabled property to enable and disable a validator. You have to programmatically determine the value for Enabled on the server side and have to learn JavaScript and the framework to update it on the browser.

Peter's Data Entry Suite offers the Enabler property to make it very simple to detect whether the validator should be enabled or not. It fully works on the client-side, avoiding you having to write any JavaScript. It supports all of the validation conditions including specialized conditions that detect attributes like visibility, enabled, readonly and much more.

Demo 1

This textbox has a RequiredTextValidator attached. However, the page was designed to ignore the contents of the textbox unless the checkbox to the left was marked. So the designer does not want any validation when it is unmarked.

To use this demo, leave the checkbox unchecked. Clear the textbox and tab off. No validation error is reported. Mark the checkbox and click Submit. Since the textbox is empty, it will now report an error.


ASP.NET Syntax for this demo

<asp:checkbox id=CheckBox1 runat="server" Text="Enter a number" />
<asp:textbox id=TextBox1 runat="server" />
<des:RequiredTextValidator id=RequiredTextValidator1 runat="server" 
  ControlIDToEvaluate="TextBox1" ErrorMessage="This field is required" Group="Group 1" >
   <ErrorFormatterContainer>
      <des:TextErrorFormatter Display="Dynamic" />
   </ErrorFormatterContainer>
   <EnablerContainer>
      <des:CheckStateCondition ControlIDToEvaluate="CheckBox1" EvaluateOnClickOrChange="False" />
   </EnablerContainer>
</des:RequiredTextValidator>
<des:button id=Button1 runat="server" Text="Submit" Group="Group 1 />

Demo 2

This textbox is disabled and enabled depending on the state of some other controls on the page (in this case, it is the radio buttons). The RequiredTextValidator attached to this textbox detects that the textbox is enabled or disabled.

Note: This demo uses a FieldStateController to enable and disable the textbox.


ASP.NET Syntax for this demo

<p>
<asp:RadioButton id=RadioButton1 runat="server" Text="Disabled" GroupName="Radio1" Checked="True" />
<asp:RadioButton id=RadioButton2 runat="server" Text="Enabled" GroupName="Radio1" />
</p>
<p>
<asp:TextBox id=TextBox2 runat="server" />
<des:RequiredTextValidator id=RequiredTextValidator2 runat="server" 
  ControlIDToEvaluate="TextBox2" ErrorMessage="This field is required" Group="Group2" >
   <ErrorFormatterContainer>
      <des:TextErrorFormatter Display="Dynamic" />
   </ErrorFormatterContainer>
   <EnablerContainer>
      <des:EnabledCondition ControlIDToEvaluate="TextBox2" /> 
   </EnablerContainer>
</des:RequiredTextValidator></p>
<p>
<des:Button id=Button2 runat="server" Text="Submit" Group="Group2"/>
<des:FieldStateControllerid =FieldStateController1runat="server"
   UseValidationGroup="True"ValidationGroup="Group2"
   ConditionFalse-Enabled="False" ControlIDToChange="TextBox2">
   <ConditionContainer>
      <des:CheckStateCondition Checked="False" ControlIDToEvaluate="RadioButton1" />
   </ConditionContainer>
</des:FieldStateController>
<des:FieldStateController id=FieldStateController2 runat="server" 
  ConditionFalse-Enabled="False" ControlIDToChange="TextBox2">
   <ConditionContainer>
      <des:CheckStateCondition ControlIDToEvaluate="RadioButton2" />
   </ConditionContainer>
</des:FieldStateController></p>