0

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.

1
  • Try to refer to the replies to related threads provided in this thread which may be helpful. Commented Mar 20, 2020 at 5:05

1 Answer 1

0

try this:

$(document).ready(function () {
    $("form").each(function () { $.data($(this)[0], 'validator', false); });
    $.validator.unobtrusive.parse("form");
});
Sign up to request clarification or add additional context in comments.

3 Comments

it still return $.validator is undefined . I found that we can load scripts via PartialView. In ASP.NET Core MVC app there is default validation Parial _ValidationScriptPartial but it's not helped. @section Scripts { @await Html.PartialAsync("~/Views/Shared/_ValidationScriptsPartial.cshtml") <script src="~/js/myScriptWhereIWantToUseValidator.js"></script> }
link _Layout.cshtml link View.cshtml link _FormPartialView.cshtml And in tyresItems.js I want to use validation methods, i.e. $('#addTyreItem-form').validate() but it's not working.
maybe it's happen because opening modal with form rendering them on the bottom of DOM, after all <script> tags too.

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.