Is it possible, using jQuery unobtrusive validation, to specify a custom validation function that is not bound to a specific field? I mean, a validation function that is triggered when the form is validated, but not targeted at a specific form field. This is in the context of ASP.NET Core validation, and I would prefer not to add any code to the form submit handler. Essentially, I would like to keep the default behavior when I click on a submit button, but besides the usual field-oriented validation also do bespoke ("global") validation too.
-
it is possible, you cann call whatever function you want on form submitjohn Smith– john Smith2018-01-09 12:50:42 +00:00Commented Jan 9, 2018 at 12:50
-
Of course I can. I should have made myself cleared: I'm talking in the context of ASP.NET Core client-side validation. Updated the question.Ricardo Peres– Ricardo Peres2018-01-09 12:51:49 +00:00Commented Jan 9, 2018 at 12:51
-
Possible to trigger a function on form valid and/or form invalid. Not possible to have a validation rule not tied to a specific field(s).Sparky– Sparky2018-01-09 19:01:21 +00:00Commented Jan 9, 2018 at 19:01
Add a comment
|
1 Answer
One way to achieve this is to have an hidden field with validation attributes:
<input type="hidden" id="val" name="val" data-val="true" data-val-custom="My custom error message" />
Then hook it to unobtrusive validation:
$.validator.addMethod('custom', function (value, element, params) {
//whatever
return false;
});
$.validator.unobtrusive.adapters.addBool('custom');
This way it will appear as if it is general, not field-specific.