比较"密码" &安培; "确认密码"在CSHTML中(使用Javascript)

时间:2014-10-24 04:26:15

标签: javascript c# asp.net-mvc-4 razor

我创建数据库并使用代码第一工作流添加表,然后创建ADO.NET Entity数据模型,当我这样做时,它自动为该数据库中的表生成模型类,

在该模型类中,它定义了密码字段,但没有确认密码字段,因为我不想存储两者,

我的Model类中的HERE代码段

    [Required(ErrorMessage = "Please provide Password", AllowEmptyStrings = false)]
    [DataType(System.ComponentModel.DataAnnotations.DataType.Password)]
    [StringLength(255, MinimumLength = 5, ErrorMessage = "Password must be 5 char long.")]
    public string Password { get; set; }

    public string PasswordSalt { get; set; }

比较以上两个字段我只是尝试使用java脚本前端验证

我只想知道如何在cshtml中使用JavaScript比较密码字段和确认密码字段 我已经尝试了很多方法来做到这一点,但仍然无法弄明白,

这是我在cshtml文件中添加的代码片段

 <script src="../../Scripts/jquery-1.7.1.min.js" type="text/javascript">
    $(function isPasswordSame()  {
        var password = document.getElementById('password');
        var confirmPassword = document.getElementById('passsword-confirm');        
        return password === confirmPassword;
    });
    </script>

这是上面模型类

的CSHTML主体定义
       <div class="editor-label">
            @Html.LabelFor(model => model.Password)
        </div>
        <div id="password" class="editor-field">
            @Html.EditorFor(model => model.Password)
            @Html.ValidationMessageFor(model => model.Password)
        </div>

         <div class="editor-label">
          <label> Confirm Password</label>
        </div>
        <div class="editor-field">
            <input type="password" id="passsword-confirm"/>
        </div>

但这不能正常工作。寻找良好的克服解决方案或任何增强功能,谢谢

这里是完整的CSHTML代码

@model  AFFEMS2_HEC.Models.HEI_REG_TABLE
    @{
    ViewBag.Title = "HEI_Registration";
    Layout = "~/Views/Shared/Login_Layout.cshtml";
     }
    <script src="../../Scripts/jquery-1.7.1.min.js" type="text/javascript">
    $(function isPasswordSame()  {
        var password = document.getElementById('password');
        var confirmPassword = document.getElementById('passsword-confirm');

        return password === confirmPassword;
    });

</script>
<script type="text/jscript"></script>
@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>User</legend>
        @Html.AntiForgeryToken()
        @if (ViewBag.Message != null)
        {
            <div style="border:solid 1px green">
                @ViewBag.Message
            </div>
        }

        <div class="editor-label">
            @Html.LabelFor(model => model.HEI_ID)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.HEI_ID)
            @Html.ValidationMessageFor(model => model.HEI_ID)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Username)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Username)
            @Html.ValidationMessageFor(model => model.Username)
        </div>

        <div  class="editor-label">
            @Html.LabelFor(model => model.Password)
        </div>
        <div id="password" class="editor-field">
            @Html.EditorFor(model => model.Password)
            @Html.ValidationMessageFor(model => model.Password)
        </div>

        <div class="editor-label">

        </div>
        <div id="passsword-confirm" class="editor-field">
             <input type="password" />
        </div>

        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

1 个答案:

答案 0 :(得分:0)

您的代码正在尝试将两个不同的元素进行比较,以查看它们是否是相同的元素。显然这总是会返回false。使用.value属性并比较它是否==(不是===)另一个元素的.value。