简体   繁体   English

如何从 JavaScript 触发 ASP.NET Core 客户端验证

[英]How to trigger ASP.NET Core client-side validation from JavaScript

Is there any way to trigger ASP.NET Core client-side validation from JavaScript?有没有办法从 JavaScript 触发 ASP.NET Core 客户端验证?

I have a Razor Pages page with a <form> that includes content like this:我有一个带有<form>的 Razor Pages 页面,其中包含如下内容:

<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?有没有办法从 JavaScript 触发这个?

I'm not actually submitting the form to the server.我实际上并没有将表单提交给服务器。 I just want to use the values in JavaScript.我只想使用 JavaScript 中的值。 But I'd like to use ASP.NET Core validation, if I can.但如果可以的话,我想使用 ASP.NET Core 验证。 I can see that I can just set the text of the validation <span> s.我可以看到我可以设置验证<span>的文本。 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.我找到了许多这样做的示例,但不适用于 ASP.NET Core 或 Razor Pages。

You can do this with unobtrusive validation.您可以通过不显眼的验证来做到这一点。 To do that, you need to include the partial view that renders the jQuery unobtrusive validation scripts.为此,您需要包含呈现 jQuery 不显眼的验证脚本的部分视图。 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:接下来,作为一个在加载时从 JavaScript 验证表单的愚蠢示例,您可以将script添加到相同的Scripts部分,在包含的部分之下:

@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>
}

Update per comments根据评论更新

@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: SiteViewModel只是:

public class SiteViewModel
{
    [Required]
    public string Name { get; set; }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM