0

我正在为一个在后端数据库中更新用户密码的网站开发密码重置功能。我可以通过输入强密码(即 Password123)成功更改用户密码!当输入的密码不够弱时,MVC 架构非常高兴,即 abc,但是当我输入不匹配的密码和确认时,即 Password123!和密码1234!然后我收到与以下行的对象引用错误相关的错误:

@using (Html.BeginForm("Reset", "Reset", new { qString = Model.QueryString } ))

这是我的完整代码:

@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
@model ......ResetModel

<script type="text/javascript">
$(document).ready(function () {
    $('#close').click(function (event) {
        closeAjax();

        event.cancelBubble = true;
        event.returnValue = false;
        event.preventDefault();
        event.stopPropagation();
    });
});

$(document).ready(function () {
    $("#index").validate({
        rules: {
            password: {
                required: true, minlength: 6
            },
            confirmpassword: {
                required: true, equalTo: "#password", minlength: 6
            }
        },
        messages: {
            password: "Please enter your new password.",
            c_password: "Please confirm your new password."

        }
    });
});

</script>
<div class="form_container">
    <div class="logo">
        <a href=""><img class="logo" src="@Url.Content("~/Content/SP_Logo_white.png")" alt="Cloud"/></a>
    </div>
        <div class="reset">
           <h3>Password Reset</h3>
         <div class="reset-help">
      <p>Forgot your password? Use the form to change or reset your password.</p>
    </div>
    @using (Html.BeginForm("Reset", "Reset", new { qString = Model.QueryString } ))
    {
    <label for="reset">UserName:</label>
    <input type="text" id="username" name="username" /><br />
    <label for="password">New Password:</label>
    <input type="password" id="password" name="password" /><br />
    <label for="confirmpassword">Confirm Password:</label>
    <input type="password" id="confirmpassword" name="confirmpassword" />
    <p><input type="submit" id="submit" value="Submit" /></p>
    }
</div>
</div>

有人可以帮我理解为什么只有当密码不匹配时才会发生这种情况,我也无法理解为什么我的javascript验证没有被设置

渲染的 HTML 如下:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>Password Reset</title>
    <link href="/Content/Site.css" rel="stylesheet" type="text/css" />
    <script src="/Scripts/jquery-1.5.1.min.js" type="text/javascript"></script>
    <script src="/Scripts/modernizr-1.7.min.js" type="text/javascript"></script>
</head>
<body>
<img src="/2.jpg" alt="Big Pic" class="bg" />
    <div class="top_40"></div>

        <section id="main">

<script type="text/javascript">
    $(document).ready(function () {
        $('#close').click(function (event) {
            closeAjax();

            event.cancelBubble = true;
            event.returnValue = false;
            event.preventDefault();
            event.stopPropagation();
        });
    });

    $(document).ready(function () {
        $("#index").validate({
            rules: {
                password: {
                    required: true, minlength: 6
                },
                c_password: {
                    required: true, equalTo: "#password", minlength: 6
                }
            },
            messages: {
                password: "Please enter your new password.",
                c_password: "Please confirm your new password."

            }
        });
    });

</script>
<div class="form_container">
    <div class="logo">
        <a href=""><img class="logo" src="/Content/SP_Logo_white.png" alt="Cloud"/></a>
    </div>
        <div class="reset">
          <h3>Password Reset</h3>
        <div class="reset-help">
          <p>Forgot your password? Use the form to change or reset your password.</p>
        </div>
<form action="/Reset/Reset?qString=qKkQBXYEsY0GthEqFIuWYfXQ1CKcUioxpbY8vfhqN9nlOcHrZdLIYzxxhtV0YK73" method="post">        <label for="reset">UserName:</label>
        <input type="text" id="username" name="username" /><br />
        <label for="password">New Password:</label>
        <input type="password" id="password" name="password" /><br />
        <label for="confirmpassword">Confirm Password:</label>
        <input type="password" id="confirmpassword" name="confirmpassword" />
        <p><input type="submit" id="submit" value="Submit" /></p>
</form>    </div>
    </div>

        </section>
        <div class="bottom_80"></div>

</body>
</html>
4

1 回答 1

1

您有几个主要问题会阻止 jQuery Validate 插件完全运行。

1)您尚未将 Validate 插件script包含到您的代码中。在你的 jquery包含之后script的某个地方,你应该有这样的东西......

 <script src="/Scripts/jquery.validate.min.js" type="text/javascript"></script>

2)您已附加.validate()$('#index'). id="index"我在您的页面上 没有看到任何地方。如果您想正常运行,则index需要成为id元素的form.validate()例子:

jQuery :

$(document).ready(function() {
    $('#formID').validate({
        ...

HTML

<form id="formID">
    ...

3)您错误地将“确认密码”字段选择.validate()c_password. 此键必须与字段的属性匹配name,这实际上confirmpassword与您的 HTML 一致。

<input type="password" id="confirmpassword" name="confirmpassword" />

工作演示:http: //jsfiddle.net/6RHDP/

于 2013-05-22T20:02:49.093 回答