ASP MVC4 + JQuery验证不起作用

时间:2012-11-21 05:17:03

标签: jquery asp.net asp.net-mvc razor jquery-validate

我已按照互联网中的步骤设置密码验证。

我正在使用ASP.NET MVC4 + razor + jquery,但验证似乎不起作用。这是我的代码:

CSHTML:

@using (Html.BeginForm("Index","Home",FormMethod.Post,new { id="resetForm"}))
{

    <ol class="round">
    <li class="one">
        <h5>Select the Application.</h5>
        @model PWDManagementCenter.Models.UserApplicationModels

        @Html.DropDownListFor(x => x.selectedUserApp, Model.userApps)
    </li>

    <li class="two">
        <h5>Input Login ID of Application</h5>
        <input name="txtLogonId"   />
    </li>

    <li class="three">
        <h5>Input the new password</h5>
        <input id="txtPwd" name="txtPwd" type="password" />
        <h6>Retype the password</h6>
        <input id="txtPwd2" name="txtPwd2" type="password" /><p />
        <input id="btnSubmit" type="submit" value="Save" />
    </li>
</ol>
}

JS in html:

<script src="/Scripts/jquery-1.7.1.js" type="text/javascript" />
<script src="/Scripts/jquery.validate.js" type="text/javascript" />
<script type="text/javascript">
    $(document).ready(function () {
        $('#resetForm').validate({
            rules: {
                txtPwd: {
                    require: true, minlength: 6, maxlength: 20
                },
                txtPwd2: {
                    require: true, equalTo: '#txtPwd', minlength: 6, maxlegth: 20
                }
            },
            messages: {
                txtPwd: {
                    require: "Password is required!",
                    minlength: "Password contains at least 6 characters",
                    maxlength: "Password contains at most 20 characters"
                },
                txtPwd2: {
                    equalTo: "Make sure input the same password as above"
                }
            }
        });


</script>

控制器:

[HttpPost]
        public ActionResult Index(UserApplicationModels model)
        {
            string appName = model.selectedUserApp;
            string id = Request.Form["txtLogonId"];
            string newPwd = Request.Form["txtPwd"];
            string newPwd2 = Request.Form["txtPwd2"];

            ......
        }

当我在调试模式下单击提交按钮时,它会跳转到此索引函数而不进行验证。

为什么呢?我错过了什么?请帮忙。

我遵循这个简单的验证demo

感谢那些回答这个问题的人。你给了我点击来解决这个问题。

  1. 脚本标签应以“</script>”结尾,而不是“/>” 所以这个“<script src="/Scripts/jquery.validate.js" type="text/javascript" />”无效 但是这个“<script src="/Scripts/jquery.validate.js" type="text/javascript" ></script>”有效

  2. 1之后,我收到运行时错误,说“jscript运行时错误对象不支持此属性或方法” 这是因为我们将jquery包括两次。

  3. 在1,2之后,验证是正常的。

2 个答案:

答案 0 :(得分:2)

  1. 除非您错误地粘贴了您选择的代码,否则您的括号/括号内容不匹配。 document.ready()功能未正确关闭 - 您错过了关闭});

  2. required不是“需要”

    $('#resetForm').validate({
            rules: {
                txtPwd: {
                    required: true, minlength: 6, maxlength: 20
                },
                txtPwd2: {
                    required: true, equalTo: '#txtPwd', minlength: 6, maxlegth: 20
                }
            },
            messages: {
                txtPwd: {
                    required: "Password is required!",
                    minlength: "Password contains at least 6 characters",
                    maxlength: "Password contains at most 20 characters"
                },
                txtPwd2: {
                    equalTo: "Make sure input the same password as above"
                }
            }
        });
    

答案 1 :(得分:1)

您似乎手动编写了jquery验证脚本,而不是依赖于ASP.NET MVC中内置的不显眼的jquery验证。如果是这种情况,请确保您的页面中从不引用jquery.validate.unobtrusive脚本。同样在ASP.NET MVC 4互联网模板中有一个包含此脚本的~/bundles/jqueryval包,因此请确保您从未将其包含在页面或布局中。

您需要的只是jqueryjquery.validate脚本。还要检查你的javascript控制台,确保没有任何错误。