6

We often come into problems with .NET validators on elements that are hidden using javascript/css (ie. display:none)

For example (there may be syntax errors but don't worry about it)

<asp:CheckBox ID="chkNewsletter" runat="server" Checked="true" />
...
<div id='newsletterOnly'>
  <asp:TextBox ID="txtEmail" runat="server" />
  <asp:RequiredFieldValidator ID="vldEmail" ControlToValidate="txtEmail" ErrorMessage="Required" runat="server" />
</div>

with JavaScript:

$('#chkNewsletter').changed(function() {
  $(this).is(':checked') ? $('#newsletterOnly').show() : $('#newsletterOnly').hide();
});

It should not validate txtEmail if it is hidden.
You can't submit the form if newsletterOnly is hidden, because the RequiredFieldValidator is still effective eventhough it is hidden :(
And you can't even see the validator error message because it is hidden

Is there any way around this?

I am trying to avoid PostBacks to improve user experience.
I wish I could modify .NET javascript to validate controls only when they are visible.

4
  • Have you tried disabling the hidden elements? I'd expect that to be the solution. Commented Mar 7, 2012 at 4:39
  • What about the possibility of using two different view models when posting to the server: one for each of the different situations? You would just need to post to two different routes. Commented Mar 7, 2012 at 4:40
  • Alternatively you would need to do your validation in the javascript, rather than on the server. Commented Mar 7, 2012 at 4:40
  • @ MAT GAL Disabling them using JS? I'd rather write my own validations then? @Eli Not sure what you mean sorry. I've made the example clearer. @ arcynum That's what I want to avoid Commented Mar 7, 2012 at 4:55

3 Answers 3

10

I wrote this as a general solution (can be used on all .NET websites).

You only need to add an OnClientClick to the submit button.

//===================================================================
// Disable .NET validators for hidden elements. Returns whether Page is now valid.
// Usage:
//   <asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClientClick="DisableHiddenValidators()" />
//===================================================================
function DisableHiddenValidators() {
  for (var i = 0; i < Page_Validators.length; i++) {
    var visible = $('#' + Page_Validators[i].controltovalidate).is(':visible');
    ValidatorEnable(Page_Validators[i], visible)
  }
  return Page_ClientValidate();
}

To use it, simply include the above javascript and add the class OnClientClick="DisableHiddenValidators()" to the submit button:

<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClientClick="DisableHiddenValidators()" />

EDIT: jQuery $(submitButton).click function didn't work on iPhone/Android. I have changed the sample code above slightly.

If anyone see anything wrong or possible improvements please comment :)

Sign up to request clarification or add additional context in comments.

3 Comments

Unless I am doing somthing wrong. This disables all validators on the page. Even visible ones?
It shouldn't. Is the visible variable set correctly for you?
Magic: Thankyou! @aximili : a small modification added below to facilitate the ValidationGroup it resides in..
0

It can also be a good idea to use Validation Groups in a situation like this. If you can group your validators then specify on the Button which validation group you need to validate against, only those validators in the group will be validated. Read more here: http://msdn.microsoft.com/en-us/library/vstudio/ms227424(v=vs.100).aspx

Comments

0

Magic: Thankyou!

Modified slightly to include the ValidationGroup it resides in..

function DisableHiddenValidators(validationGroup) {
    for (var i = 0; i < Page_Validators.length; i++) {
        var isVisible = $('#' + Page_Validators[i].controltovalidate).is(':visible');  
        var isValGrp = (Page_Validators[i].validationGroup === validationGroup);
        ValidatorEnable(Page_Validators[i], (isValGrp && isVisible));  //Turn on only if in Validation group and IsVisible = true
    }
    return Page_ClientValidate(validationGroup);
}

To add Custom DisableValidators method to the click event tree:

$('#myButtonControlId').on("click", function(e) { DisableHiddenValidators('Send'); });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.