【问题标题】:MVC Jquery Validation Password Confirm not workingMVC Jquery验证密码确认不起作用
【发布时间】:2013-11-15 02:18:33
【问题描述】:

这是代码,我在布局中加载了所有 Jquery 脚本,但是,当我输入与确认密码不同的密码时。光标移出确认密码字段后不生效,我不知道为什么?这是 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 验证。

感谢您的帮助

【问题讨论】:

    标签: jquery asp.net-mvc password-confirmation


    【解决方案1】:

    您可以轻松简单地做到这一点:

    • 在 RegistrationViewModel 中,添加命名空间 System.ComponentModel.DataAnnotations
    • 为密码和确认密码添加验证属性

      [Required(ErrorMessage = "Password required")]
      [StringLength(int.MaxValue, MinimumLength = 6, ErrorMessage = "Password is at least 6 characters.")]
      public string Password { get; set; }
      
      [CompareAttribute("Password", ErrorMessage = "Password doesn't match.")]
      public string ConfirmPassowrd { get; set; }
      
    • 在你的剃须刀中,使用简单,无需添加jquery来处理确认密码

      <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>
      <div class="editor-field" id="confirmPassword">
         @Html.EditorFor(model => model.ConfirmPassword)
         @Html.ValidationMessageFor(model => model.ConfirmPassword)
      </div>
      

    希望有帮助!

    【讨论】:

    • 如果我的模型没有 ConfirmPassword 属性怎么办?我只需要即时比较 Password 和 ConfirmPassword,然后只有密码存储在数据库中。
    • 您好新手,ConfirmPassword 只是一个比较属性,不需要存储。如果您不需要它,请在 ConfirmPassword 文本框上使用 Jquery 事件 keyup 或 keydown。它比 $("CredentialForm").validate 函数更有效。
    • 这个答案没有帮助。 OP 想知道为什么他的代码不起作用,而不是解决方法或更好的解决方案。
    【解决方案2】:

    如果你在“RegistrationViewModel”类中添加了属性,上面的javascript代码就不需要了。

    你最好复习一下原生的MVC示例,对学习者来说真是一大宝。

    【讨论】:

      猜你喜欢
      • 2014-02-14
      • 2023-04-08
      • 2021-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-21
      • 2020-09-03
      • 1970-01-01
      相关资源
      最近更新 更多