Enabling / disabling RequiredValidator on the client side / CustomValidator does not work

I have a drop-down list where the user selects a country. This is a required field".

Next to it there is a text box called State. If the user selects USA, the state of the field is required. If the user selects, for example, Sweden, a state is not required since there are no states in Sweden.

Code example:

<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 is for you: how can I perform authentication using CustomValidator when it is empty?

Or simplify: how can I conditionally create a RequiredValidator fire?

Or the simplest: how can I enable / disable RequiredValidator on the client side?

+6
customvalidator requiredfieldvalidator
source share
2 answers

Try to do this with javascript to enable and disable validators

 ValidatorEnable(RequiredFieldValidatorId, false); 

Mark this question that I answered .

+10
source share

Asp.net has a javascript client function for managing validators, the "ValidatorEnable" function,

 ValidatorEnable(RequiredFieldValidatorId, false); 

You can call it simply using javascript, you must send the validator object to the function (and not just its identifier).

 if (x==y) { ValidatorEnable($('#<%=rfvFamily.ClientID %>'), false); } else { ValidatorEnable($('#<%=rfvFamily.ClientID %>'), true); } 

or

 if (x==y) { ValidatorEnable(document.getElementById("<%=rfvFamily.ClientID %>", false); } else { ValidatorEnable(document.getElementById("<%=rfvFamily.ClientID %>", true); } 

full documentation at: http://msdn.microsoft.com/en-us/library/Aa479045#aspplusvalid_clientside

Another way is to set CausesValidation = "false" in your DropDownList so that the validators do not block the postback when the DropDownList record is changed.

(*) Remember that this function is intended for the client side, to disable the server-side validator, you must also disable the validator when posting the page back.

 if (IsPostBack){ if (x==y) { rfvFamily.Enabled = false; } } 
0
source share

All Articles