Skip to content

Instantly share code, notes, and snippets.

@danielgreen
Last active October 26, 2023 14:19
Show Gist options
  • Save danielgreen/5669831 to your computer and use it in GitHub Desktop.
Save danielgreen/5669831 to your computer and use it in GitHub Desktop.
The jQuery Validation plugin does not validate hidden fields by default. Here is how to get around that.
// By default, the jQuery Validation plugin ignores hidden fields. You may want them to be validated however.
// The default 'ignore' setting is ':hidden'
// See https://github.com/jzaefferer/jquery-validation/issues/189
// Attach a validator to a particular form, with a blank 'ignore' setting.
// If you are using Unobtrusive Validation then you can't do this, as the Unobtrusive script initialises the plugin for you.
// Instead, you must either amend the setting on the validator once it's attached to the form,
// or amend the default setting before the validator is attached (read on to see how).
$("MyForm").validate({ ignore: "" });
// Amend the 'ignore' validation setting on a particular form that already has a validator attached.
$("#MyForm").validate().settings.ignore = "";
// Alternative syntax:
$("#MyForm").data("validator").settings.ignore = "";
// Amends the default ignore setting, affects all forms on the page that do not yet have a validator attached.
$.validator.setDefaults({ ignore: "" });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment