首页 > 解决方案 > asp.net Core 2.1 MVC 验证器不工作

问题描述

我有以下页面:

@model LogonModel
<div class="login-modal modal">
<div class="modal-dialog modal-dialog-centered" role="document">
    <div class="modal-content">
        <div class="modal-header">
            <h5 class="modal-title">Welcome</h5>
        </div>

        <div class="modal-body">
            @if (!ViewData.ModelState.IsValid)
            {
                <div class="alert alert-danger">
                    @foreach (var modelState in ViewData.ModelState)
                    {
                        <ul><li>@Html.ValidationMessage(modelState.Key, modelState.Value)</li></ul>
                    }
                </div>
            }

            <div class="row">
                <div class="col-md-12">
                    <p>Don't have an account? <a href="#">Contact us now.</a></p>

                    <form asp-action="Login" class="login-form">
                        <div asp-validation-summary="ModelOnly" class="validation-summary text-danger"></div>

                        <div class="form-group">
                            <label asp-for="EmailAddress" class="control-label">Email Address</label>
                            <input asp-for="EmailAddress" class="form-control" />
                            <span asp-validation-for="EmailAddress" class="text-danger"></span>
                        </div>
                        <div class="form-group">
                            <label asp-for="Password" class="control-label"></label>
                            <input type="password" asp-for="Password" class="form-control" />
                            <span asp-validation-for="Password" class="text-danger"></span>
                        </div>

                        <div class="form-group">
                            <input type="submit" value="Sign in" class="btn btn-default login-submit" />
                        </div>

                        <div class="form-check">
                            <input type="checkbox" class="form-check-input" asp-for="RememberUser">
                            <label class="form-check-label" asp-for="RememberUser">Remember my account details</label>
                        </div>

                        <div class="form-group">
                            <a href="#">Login using a subscription ID</a><br />
                            <a href="#">Forgot your password?</a>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>

最初,发给控制器的帖子只是在提交按钮上,验证器工作。即省略电子邮件,验证器将显示错误消息。出于其他原因。当我按下提交按钮时,我在转到我的控制器之前调用一个 jQuery 函数,在我的 javascript 中,我做的第一件事是:

var loginForm = $(".login-form");

    if (loginForm.valid()) {…..

这似乎不起作用,因为即使我关闭电子邮件,我的 javascript 也会被调用,它仍然会通过我的“if”语句中的代码。

我需要做什么才能让验证器再次工作?

标签: jqueryasp.net-core-mvc

解决方案


jQuery Validation 仅在输入被聚焦/修改或实际提交表单时触发(您的代码在此之前运行)。在任何一种情况下,该valid方法仅报告已触发验证的输入的状态。换句话说,true即使所需输入为空(如果尚未验证),它也有可能返回。

总而言之,您首先需要调用validate来触发完整的表单验证。

loginForm.validate();
if (loginForm.valid()) {
    ...
}

推荐阅读