Enable Unobtrusive JQuery Validation on Hidden Input Fields in ASP.NET MVC

After release of JQuery Validation Plugin 1.9 hidden elements on form are ignored by default from validation.

The problem is that you sometimes need a validation on these hidden fields.

However there is a nice workaround.

Make sure to put

$.validator.setDefaults({ ignore: '' });

not inside

$(document).ready

This will force client side validation to work even on hidden fields.

But what if you change value of hidden input from JQuery and want validation to perform after this change?

The code above will not help in this case.

I will show you a nice trick for this on example hidden input #SomeInput :

 $("#SomeInput").val("newValue").trigger("change");
 $("#SomeInput").valid();

We must call method valid after apply change to hidden input to force validation to perform after manually changing hidden input value.

If you like this article don’t forget to subscribe to this blog and make sure you don’t miss new upcoming blog posts.

Leave a Reply