1

我在密码验证工作时遇到了一些麻烦,我认为 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>
4

1 回答 1

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>

希望这可以帮助。

于 2013-05-23T10:04:04.303 回答