1

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.

3
  • it is possible, you cann call whatever function you want on form submit Commented 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. Commented 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). Commented Jan 9, 2018 at 19:01

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.