Disable / enable requiredFieldValidators with javascript and server support

I have drop down (dropdown2) which requires IF , there is something in it, but these options are controlled by ajax from another drop-down list (dropdown1). Sometimes dropdown2 will be empty, in which case I cannot require it. So I can turn off requiredFieldValidators in javascript by calling this ...

 ValidatorEnable(document.getElementById(validatorId), false); 

This works fine, but the server still runs the requiredFieldValidator logic. Does anyone know how I can make the server not check if the validator is configured on the false client side?

+7
source share
5 answers

Why don't you just use the client side validator? You make your work much harder by doing this. If you have access to it through clients, why are you worried about deleting it from the server?

The only thing I can think of is to create a hidden field and set it on the client side, and then when you do a postback to check this value and disable / activate the validator.

For example, after this:

JS:

 ValidatorEnable(document.getElementById(validatorId), false); var hidden = document.getElementById(hiddenID); hidden = "1"; 

Then in your load event:

 If (hidden = "1") then validator.enabled=false end if 

Take a look at this post, very similar to yours: ASP.NET - how to stop checking an idle server

+8
source

DISABLE

 document.getElementById("<%=ReqVal.ClientID%>").style.visibility = "hidden"; document.getElementById("<%=ReqVal.ClientID%>").enabled = false; 

Turn on

 document.getElementById("<%=ReqVal.ClientID%>").style.visibility = "visible"; document.getElementById("<%=ReqVal.ClientID%>").enabled = true; 
+9
source

Require validators to be entered into the DOM as span elements.

If you are using JQUERY, get the element using the jQuery selector, then select the DOM element from your selection.

Here is an example:

Suppose you have a validator id = "MyReqValidator" request.

In your javascript file you will do:

 //The jQuery Element: jqValidator = $("span[id$=MyReqValidator]"); //No the DOM element. This is what document.getElementById would return. domValidator = jqValidator.get(0) //Now enable your validator: ValidatorEnable(validator, true); 

All in one line of code

 ValidatorEnable( $("span[id$=MyReqValidator]").get(0), true); 
+4
source
  ValidatorEnable($("[id$='RegularExpressionValidator4']")[0], true); 
0
source

So, I didnโ€™t get JonH's answer to work, and the rest is only on the client side. So this is my solution:

To disable Client-side RequiredFieldValidator:

 ValidatorEnable(document.getElementById("rfv"), false); 

To disable RequiredFieldValidator on the server side, you can override the Validate () method as follows:

 Public Overrides Sub Validate() Dim disable_rfv As Boolean = input_to_check <> 1 rfv.Enabled = disable_rfv MyBase.Validate() End Sub 
0
source

All Articles