Jonathan Wood
Jonathan Wood

Reputation: 67175

Run custom validation script when form is submitted

I have an ASP.NET Core Razor Pages application with a page that requires some custom validation. I want this validation to run on the client using custom JavaScript.

How can I ensure my validation script runs when the form is submitted? It also needs to run if $form.validate() and $form.valid() are called manually.

Googling around, I found a few examples. But they seem very old and use technologies such as WebForms.

Can someone offer some suggestions or refer me to resources that cover this issue in detail?

NOTE: My validation will check that a value is provided for at least one of a group of input elements. So, if possible, I just want my new validation to run and not be associated with any one input.

UPDATE

Based on information I found on the Web, I came up with the following.

$.validator.addMethod("validGroups", function (value, element, params) {
    alert('Test');
    return false;
}, 'Error Message');

jQuery.validator.unobtrusive.adapters.add('validGroups', function (options) {
    options.rules['validGroups'] = {};
    options.messages['validGroups'] = options.message;
});

But I tried a number of variations on this but none of them resulted in this validator being run.

Upvotes: 1

Views: 272

Answers (0)

Related Questions