jQuery将焦点移至验证错误消息

时间:2011-12-15 14:30:47

标签: jquery asp.net-mvc-3 jquery-validate

我正在尝试使用Jquery将焦点转移到验证错误消息。通过将焦点移动到错误消息,残疾屏幕阅读器将向用户读取错误消息。我正在使用JQuery验证:

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

HTML:

@Html.ValidationSummary(true, "Invalid Login! Please correct the errors and try again.")
@using (Html.BeginForm("LogOn", "Account", FormMethod.Post, new { id = "logonForm" }))
{
    @Html.AntiForgeryToken()
    <div>
        <fieldset>
            <legend>Account Information</legend>
            <div class="editor-label">
                @Html.LabelFor(m => m.UserName)
            </div>
            <div class="editor-field">
                @Html.TextBoxFor(m => m.UserName, new { autocomplete = "off" })
                @Html.ValidationMessageFor(m => m.UserName)
            </div>
            <div class="editor-label">
                @Html.LabelFor(m => m.Password)
            </div>
            <div class="editor-field">
                @Html.PasswordFor(m => m.Password, new { autocomplete = "off" })
                @Html.ValidationMessageFor(m => m.Password)
            </div>
            <p>
                <input type="submit" value="Log On" id="btnLogOn" />
            </p>
        </fieldset>
    </div>
}

JQuery的:

$("#logonForm").validate({
            invalidHandler: function (form, validator) {
                var errors = validator.numberOfInvalids();
                alert(errors);
            }
        });

无效处理程序永远不会触发。我从来没有打过警报声明。有什么想法吗?

以下是从Razor View Engine生成的HTML:

<form action="/Account/LogOn" id="logonForm" method="post"><input name="__RequestVerificationToken" type="hidden" value="VsOtMMJFUi6nvl38UAqVWsNkteNOaWBRKTUPAHgvn2xRKqBiZDbF//Glbl9IXJUkQssdP7wDk8MKskmN8HYtem+HSDViqRioxEatXs2oqzftBsdhzsrFgZ+akOPb0OZzFMer+A==" />        <div>

            <fieldset>

                <legend>Account Information</legend>

                <div class="editor-label">

                    <label for="UserName">Username</label>

                </div>

                <div class="editor-field">

                    <input autocomplete="off" data-val="true" data-val-regex="Malicious Username" data-val-regex-pattern="^[a-zA-Z0-9\.\@\w-\w_]+$" data-val-required="The Username field is required." id="UserName" name="UserName" type="text" value="" />

                    <span class="field-validation-valid" data-valmsg-for="UserName" data-valmsg-replace="true"></span>

                </div>

                <div class="editor-label">

                    <label for="Password">Password</label>

                </div>

                <div class="editor-field">

                    <input autocomplete="off" data-val="true" data-val-required="The Password field is required." id="Password" name="Password" type="password" />

                    <span class="field-validation-valid" data-valmsg-for="Password" data-valmsg-replace="true"></span>

                </div>

                <p>

                    <input type="submit" value="Log On" id="btnLogOn" />

                </p>

            </fieldset>

        </div>

</form>

1 个答案:

答案 0 :(得分:1)

看起来您没有任何必填字段。您可以将它们添加到规则中,也可以在字段中添加“必需”类。

此外,您似乎正在将ASP.NET验证与jQuery验证混合使用; MS提供两者,但你只使用一个。