コードは次のとおりです。すべての Jquery スクリプトをレイアウトでロードしますが、confirm-password とは異なるパスワードを入力すると、カーソルがパスワードの確認フィールドから移動した後、検証されません。理由がわかりません。これはRenderbodyのビューです
USBBook.Models.RegistrationViewModel
@{
ViewBag.Title = "Registration";
}
<script type="text/javascript">
$(document).ready(function () {
$("CredentialForm").validate ({
rules: {
password: { required: true,
minlength: 5 },
confirm-password: {
required: true,
minlength: 5,
equalTo: "#password" }
},
messages: {
password: {
required: "Please provide password",
minlength: "Password must be at least 5 characters long" },
confirm-password: {
required: "Please provide password",
minlength: "Password must be at least 5 characters long",
equalTo: "Please check password" }
}
});
});
</script>
<h2 style="text-align: center">
Registration</h2>
<h3 style="margin-left: 85px">
Account Information</h3>
<hr style="width: 70%; margin: 0px 0px 0px 85px" />
@using (Html.BeginForm("Create", "Credential", new { id = "CredentialForm" }))
{
@Html.ValidationSummary(true)
<fieldset>
@Html.HiddenFor(model => model.AccountID)
<div class="editor-label">
<span class="red">*</span>@Html.LabelFor(model => model.Email)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Email)
@Html.ValidationMessageFor(model => model.Email)
</div>
<div class="editor-label">
<span class="red">*</span>@Html.LabelFor(model => model.Password)
</div>
<div class="editor-field" id="password">
@Html.EditorFor(model => model.Password)
@Html.ValidationMessageFor(model => model.Password)
</div>
<div class="editor-label">
<span class="red">*</span>Confirm Password
</div>
<input id="confirm-password" class="editor-field" type="password" />
<p>
<input class="button" style="margin-left: 55px" type="submit" value="Submit" />
</p>
</fieldset>
これらは、レイアウトでレンダリングされるスクリプトです。
<script src="../../Scripts/jquery-1.10.2.min.js" type="text/javascript"></script>
<script src="../../Scripts/jquery.validate.min.js" type="text/javascript"></script>
<script src="../../Scripts/jquery.validate.unobtrusive.min.js" type="text/javascript"></script>
パスワードを検証し、パスワード フィールドを確認する必要があります。電子メールは MVC によって検証されます。
手伝ってくれてありがとう