Enable/disable RequiredValidator on client-side / CustomValidator not firing

Posted by Simeon on Stack Overflow See other posts from Stack Overflow or by Simeon
Published on 2010-04-23T14:44:47Z Indexed on 2010/04/23 16:33 UTC
Read the original article Hit count: 406

I've got a drop-down where the user selects a Country. It is a required "field".

Next to it, there is a textfield named State. If the user selects US, then the field State is required. If the user selects e.g. Sweden, the State is not required, since Sweden has no states.

Example code:

<asp:DropDownList runat="server" ID="Country"></asp:DropDownList>
<asp:RequiredFieldValidator ControlToValidate="Country"
                runat="server" Display="Static" ErrorMessage="Required field" />

<asp:TextBox runat="server" ID="State"></asp:TextBox>
<asp:CustomValidator ClientValidationFunction="DoesntGetFiredIfStateIsEmpty"
                runat="server" Display="Static" ErrorMessage="Required field" />

<!-- SO, RATHER THIS TOGETHER WITH CONDITIONAL FIRING -->
<asp:RequiredFieldValidator ControlToValidate="State"
                runat="server" Display="Static" ErrorMessage="Required field" />

My question to you is: How can I make this CustomValidator fire validation when it is empty?

Or put simplier: How can I make a RequiredValidator fire conditionally?

Or simplest: How can I enable/disable a RequiredValidator on client-side?

© Stack Overflow or respective owner

Related posts about ASP.NET

Related posts about requiredfieldvalidator