密码确认未被解雇

时间:2013-05-23 09:51:32

标签: javascript jquery validation html.beginform

我在密码验证工作时遇到了一些麻烦,我认为现在正确定义了javascript,但我无法验证表单是否成功,因为无论错误如何都会不断发布。

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

<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>
<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 () {
    $("#resetForm").validate({
        rules: {
            password: {
                require: true, 
                minlength: 6
            },
            confirmpassword: {
                require: true,                     
                minlength: 6,
                equalTo: "#password"
            }
        },
        messages: {
            password: {
                require: "You must enter a password!",
                minlength: "Password must contain at least 6 characters"
                },
            confirmpassword: {
                require: "You must enter a password confirmation!",
                minlength: "Password must contain at least 6 characters",
                equalTo: "Password and Password Confirmation fields do not match!"
                }
        }
    });
});

这是HTML:

<div class="form_container">
<div class="logo">
    <a href=""><img class="logo" src="@Url.Content("~/Content/SP_Logo_white.png")" alt="SmartPlant 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, @id = "resetForm" } ))
    {
    <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>

1 个答案:

答案 0 :(得分:0)

如果输入的数据不正确,您需要创建验证表单客户端的函数,该函数将返回值“false”,因此不会提交from。这不涉及PHP,以便在加载PHP之前捕获它。 您也可以在服务器端进行验证检查。

   function Validate()
    {

        //insert javascript validation check

                return false;
                }

 <form onsubmit="return Validate();"  action="your.php" method="post" name="your name" >

 <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>

希望这有帮助。