title: Enabling/Disabling RegularExpressionValidator in code behind
body: <p>I have a problem with RegularExpressionValidator connected with TextBox:</p>
  
  <pre><code>&lt;asp:TextBox ID="tbPRCOD" runat="server" Font-Bold="True" ForeColor="Green" BackColor="White" BorderStyle="None" Width="50%"&gt;&lt;/asp:TextBox&gt;
  
  &lt;asp:RegularExpressionValidator id="revPRCOD" runat="server" SetFocusOnError="True"
                                   ErrorMessage="&lt;%$ Resources:GlobalTranslations, max20char %&gt;" Display="Dynamic"
                                   ControlToValidate="tbPRCOD" BackColor="Transparent" Font-Bold="True"
                                   Font-Underline="True" ForeColor="Red" ValidationExpression="^[a-zA-Z0-9]{0,20}$"&gt;
  </code></pre>
  
  <p></p>
  
  <p>On page I have Drop down list:</p>
  
  <pre><code>&lt;asp:DropDownList ID="cmbIDFAM" runat="server" Width="98%" SkinID="mandatoryCombo" Font-Size="X-Small" AutoPostBack="true" &gt; &lt;/asp:DropDownList&gt;&lt;/td&gt;
  </code></pre>
  
  <p>At start validator is enabled (if string in text box will be over 20 characeters then validator shows validate error). If I change selected item in DropDownList to item with value 5 then I want to turn off validation because for this index in DropDownList, I want to write in TextBox (tbPRCOD) more than 20 characters:</p>
  
  <pre><code>        Protected Sub cmbIDFAM_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmbIDFAM.SelectedIndexChanged
          If getValueFromCombo(cmbIDFAM) = 5 Then
              tbPRCOD.MaxLength = 100
              revPRCOD.Enabled = False
  
          Else
              tbPRCOD.MaxLength = 20
              revPRCOD.Enabled = True
          End If
  
      End Sub
  </code></pre>
  
  <p>Everything works fine until I change cmbIDFAM DropDownList to 5th item. I put in TextBox more than 20 characters, and now RegularExpressionValidator is disabled so it don't show error in validate. When I change item in ddl then in TextBox is more than 20 characters and validator is enabled, but error doesn't show in page. Why? How to force validation? I also use function to force Validation, after change item in ddl:</p>
  
  <pre><code>revPRCOD.Validate()
  </code></pre>
  
  <p>This won't help in this situation. Thanks for Help.
  Mat.</p>