RequiredFieldValidator still checks when hidden

I have 2 fields that I need to check if they appear on the screen. When the form is initially loaded, they are hidden, and they remain hidden unless an item is selected from the DropDown window. After the value is selected, 2 fields appear and then check if it works correctly. However, if a different value is selected that does not display these 2 fields, they are still validated and prevent the page from being submitted. Any ideas on how I can achieve this?

function DisplayOutageDates() {
    if ($('#ddImpact').val() == "Service Affecting") { 
    $('#outageDates').css('display',''); 
    document.getElementById('txtOutageStartDate').Visible = true;
    document.getElementById('RFVOutageStartDate').Visible = true;
    } else {
    $('#outageDates').css('display','none');
    document.getElementById('txtOutageStartDate').Visible = false;
    document.getElementById('RFVOutageStartDate').Visible = false;
    }
}

<asp:RequiredFieldValidator ID="RFVOutageStartDate" runat="server" 
    ControlToValidate="txtOutageStartDate" SetFocusOnError="true"
    ErrorMessage="Please enter the Outage Start Date" />

      

+3


source to share


2 answers


You can use:

ValidatorEnable(val, enable): 
Takes a client-validator and a Boolean value. 
Enables or disables a client validator. 
Being disabled will stop it from evaluating and it will always appear valid. 

      

Found on msdn .

Using Javascript would look like this:



ValidatorEnable(document.getElementById('<%=Validator1.ClientID%>'), state);
//where state would be a boolean 

      

In JQuery it would look like this:

 ValidatorEnable($("#<%= Validator1.ClientID %>")[0], state);

      

How to find it here: http://codeclimber.net.nz/archive/2008/05/14/how-to-manage-asp.net-validation-from-javascript-with-jquery.aspx

+2


source


I think you need to show and hide the controls by Validator

showing and hiding the input controls.




Update

If you hide the controls Validator

using display:none;

, they still get rendered and participate in the validation process. You need to hide them by setting the property Visible

to false. Thus, they will not get any participation in the verification process.

+1


source







All Articles