kubala9
kubala9

Reputation: 11

Why jQuery validation funcs are not working in ASP.NET MVC Core app?

I have ASP.NET Core MVC app and I want to use jQuery client-side validation. I import scripts in _Layout.cshtml and I see them in Source files:

 <script src="~/lib/jquery/dist/jquery.js"></script>
 <script src="~/lib/jquery-validation/dist/jquery.validate.js"></script>
 <script src="~/lib/jquery-validation/dist/additional-methods.js"></script>
 <script src="~/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.js"></script>

Unobtrusive validation working good when I'm using simple submit button with MVC form (asp-action & asp-controller). Errors appears and it's OK. But the problem occurs when I'm using Ajax for submit form to controller and I want call jQuery validation functions like .validate(), $.validator.unobtrusive.parse('form') etc.

Then there are errors in Console like:

$(...).validate is not a function
$.validator is undefined

I tried to put scripts in views, directly before the script in which I use them. I put my script in _Layout.cshtml too, but it didn't help.

Upvotes: 0

Views: 686

Answers (1)

kgajjar20
kgajjar20

Reputation: 304

try this:

$(document).ready(function () {
    $("form").each(function () { $.data($(this)[0], 'validator', false); });
    $.validator.unobtrusive.parse("form");
});

Upvotes: 0

Related Questions