As a corollary to this question, how would I configure jQuery Validate to validate some hidden fields in a form, but not all of them?
jQuery Validate ignores hidden fields by default, which is sort of an issue in ASP.NET projects. For instance, a page might have a form with both checkbox helpers (which generate hidden inputs
), as well as custom dropdowns which may utilize many hidden inputs.
$(document).ready(function() {
$('#myform').validate({
ignore: [],
// any other options and/or rules
});
});
Using the config object like so, is there any way that I can set this up to allow me to pick and choose which specific hidden inputs to validate, without having to specify every individual input the plugin has to ignore?
Alternatively, is there a validation event hook that I could leverage in this scenario?
The default value for the ignore
property is ":hidden"
. You could simply use the :not()
pseudo class to negate a specific class from being ignored.
For instance, if you use the selector ":hidden:not(.do-not-ignore)"
, jQuery validate will ignore all hidden element except for hidden elements with the class .do-not-ignore
:
$('#myform').validate({
ignore: ':hidden:not(.do-not-ignore)',
// any other options and/or rules
});
In doing so, you can add the class .do-not-ignore
to your hidden elements that you still want to validate.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With