Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save danielgreen/5670480 to your computer and use it in GitHub Desktop.
Save danielgreen/5670480 to your computer and use it in GitHub Desktop.
When using jQuery Validate, you may want to run some code when it detects that the form is invalid. When using Unobtrusive Validation, you don't have the ability to set invalidHandler directly when initialising the Validate plugin, so here is a workaround.
// If using Unobtrusive Validation then that library initialised the Validate plugin
// on our behalf, so we missed the chance to set invalidHandler. Even if we were to
// set invalidHandler now via the settings object it would have no effect, due to the
// way that Validate works internally. Instead, we can do the following:
$("#MyForm").bind("invalid-form.validate", function () {
// Do something useful e.g. display the Validation Summary in a popup dialog
$("div.ValSummary").dialog({
title: "Information",
modal: true,
resizable: false,
close: function (event, ui) {
$(event.target).remove().appendTo("#MyForm");
},
buttons: {
"OK": function () { $(this).dialog("close"); }
}
});
});
@benmccallum
Copy link

benmccallum commented May 29, 2019

Nice, except it fires twice for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment