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.
Different types of client-side validation Built-in form validation has better performance than JavaScript, but it is not as customizable as JavaScript validation. JavaScript validation is coded using JavaScript. This validation is completely customizable, but you need to create it all (or use a library).
HTML form validation can be done by JavaScript.
Scripting languages such as JavaScript and VBScript are used for client-side validation. In this kind of validation, all the user input validation is done in user's browser only.
You can do this with unobtrusive validation. To do that, you need to include the partial view that renders the jQuery unobtrusive validation scripts. To do that, add the following to the bottom of your view:
@section Scripts {
// You can find the partial in ~/Views/Shared
@{ await Html.RenderPartialAsync("_ValidationScriptsPartial"); }
}
Next up, as a silly example to validate the form from JavaScript on load, you can add a script
to that same Scripts
section, underneath the inclusion of the partial:
@section Scripts {
@{ await Html.RenderPartialAsync("_ValidationScriptsPartial"); }
<script type="text/javascript">
$(function () {
$('#formID').validate();
if ($('#formID').valid() === true) {
console.log("valid");
} else {
console.log("invalid");
}
});
</script>
}
@model SiteViewModel
<form asp-action="Index" id="formID" method="post">
<input asp-for="Name" />
<span asp-validation-for="Name"></span>
<input type="submit" />
</form>
@section Scripts {
@{ await Html.RenderPartialAsync("_ValidationScriptsPartial"); }
<script type="text/javascript">
$(function () {
$('#formID').validate();
if ($('#formID').valid() === false) {
console.log("invalid");
} else {
console.log("valid!");
}
});
</script>
}
where SiteViewModel
is just:
public class SiteViewModel
{
[Required]
public string Name { get; set; }
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With