Neil Knight
Neil Knight

Reputation: 48547

RequiredFieldValidator still validates when hidden

I have 2 fields that I need to validate, if they are displayed on the screen. When the Form initially loads, they are hidden and they will stay hidden unless an item is selected from a DropDown box. Once the value is selected, the 2 fields appear and then the validation is working correctly. However, if another value is selected that doesn't make these 2 fields appear, they are still being validated and not allowing the page to submit. 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" />

Upvotes: 3

Views: 2221

Answers (2)

Akram Shahda
Akram Shahda

Reputation: 14781

I guess you need to show and hide the Validator controls as showing and hiding the input controls.


Update

If you hide the Validator controls using display:none; They still get rendered and involved in the validation process. You need to hide them by setting the Visible property to false. This way they won't get rendered neither involved in the validation process.

Upvotes: 1

Storm
Storm

Reputation: 682

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 this would look like:

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

In JQuery this would look like:

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

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

Upvotes: 2

Related Questions