2012-11-21 103 views
1

我已经按照Internet中的步骤设置了密码验证。ASP MVC4 + JQuery验证不起作用

我使用ASP.NET MVC4 +剃刀+ jQuery的,但验证好像不work.Here是我的代码:

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在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两次。

1,2后,验证是正常的。

+0

你有没有考虑不显眼的验证(http://bradwilson.typepad.com /blog/2010/10/mvc3-unobtrusive-validation.html)?不回答你的问题,但使处理客户端和服务器验证非常容易。 – HackedByChinese

+0

这不是我想要的。请参阅我的演示链接。这是简单而纯粹的jQuery。这就是我想要的 – DerekY

+0

我明白你想要什么,只是一个供参考。 UV使用jQuery和jQuery验证,它只是增加了jQuery验证,所以你不必做你现在挣扎的手动布线。 – HackedByChinese

回答

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" 
          } 
         } 
        }); 
    
+0

是的。但修复问题1和2后。它也不起作用。 – DerekY

+0

已解决。谢谢 – DerekY

1

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

所有你需要的是jqueryjquery.validate脚本。同时检查你的JavaScript控制台,以确保没有任何错误。

+0

我只包含jquery和jquery.validate,从不包含不显眼的东西。任何想法? – DerekY

+0

然后,这应该工作,假设你没有一些JavaScript错误。你看过JavaScript控制台吗?你看过你的javascript调试工具的网络选项卡,以确保在引用脚本时没有404错误?你确定你的视图或你的布局中没有引用任何包吗? –

+0

你的意思是“你的视图或你的布局中没有引用过任何包?”。顺便说一句,哪里可以看到VS2010中的JavaScript控制台? – DerekY