Is there any way to trigger ASP.NET Core client-side validation from JavaScript?
I have a Razor Pages page with a <form>
that includes content like this:
<div class="row">
<div class="col-md-6">
<label asp-for="LocationModel.Location" class="control-label"></label>
<input asp-for="LocationModel.Location" class="form-control" />
<span asp-validation-for="LocationModel.Location" class="text-danger"></span>
</div>
<div class="col-md-6">
<label asp-for="LocationModel.LoadsPerMonth" class="control-label"></label>
<input asp-for="LocationModel.LoadsPerMonth" class="form-control" />
<span asp-validation-for="LocationModel.LoadsPerMonth" class="text-danger"></span>
</div>
</div>
If I submit the form, any validation errors are highlighted and displayed. Is there any way to trigger this from JavaScript?
I'm not actually submitting the form to the server. I just want to use the values in JavaScript. But I'd like to use ASP.NET Core validation, if I can. I can see that I can just set the text of the validation <span>
s. Maybe I could do that if I knew how to make the control border red the way the validation does.
I found a number of examples that do this, but not for ASP.NET Core or Razor Pages.
$('#myform').validate()
. – Valorize