2013-11-15 54 views
0

下面是代碼,我在佈局中加載所有Jquery腳本,但是,當我輸入與confirm-password不同的密碼時。光標移出確認密碼字段後不驗證,我不知道爲什麼?這是RenderbodyMVC Jquery驗證密碼確認不起作用

USBBook.Models.RegistrationViewModel 
@{ 
    ViewBag.Title = "Registration"; 
} 
<script type="text/javascript"> 
    $(document).ready(function() { 
     $("CredentialForm").validate ({ 
      rules: { 
        password: {   required: true, 
             minlength: 5 }, 

        confirm-password: { 
             required: true, 
             minlength: 5, 
             equalTo: "#password" } 
      }, 
      messages: { 
        password: { 
             required: "Please provide password", 
             minlength: "Password must be at least 5 characters long" }, 

        confirm-password: { 
             required: "Please provide password", 
             minlength: "Password must be at least 5 characters long", 
             equalTo: "Please check password" } 
      } 
     }); 
    }); 
</script> 
<h2 style="text-align: center"> 
    Registration</h2> 
<h3 style="margin-left: 85px"> 
    Account Information</h3> 
<hr style="width: 70%; margin: 0px 0px 0px 85px" /> 
@using (Html.BeginForm("Create", "Credential", new { id = "CredentialForm" })) 
{ 
@Html.ValidationSummary(true) 
<fieldset> 
    @Html.HiddenFor(model => model.AccountID) 
    <div class="editor-label"> 
     <span class="red">*</span>@Html.LabelFor(model => model.Email) 
    </div> 
    <div class="editor-field"> 
     @Html.EditorFor(model => model.Email) 
     @Html.ValidationMessageFor(model => model.Email) 
    </div> 
    <div class="editor-label"> 
     <span class="red">*</span>@Html.LabelFor(model => model.Password) 
    </div> 
    <div class="editor-field" id="password"> 
     @Html.EditorFor(model => model.Password) 
     @Html.ValidationMessageFor(model => model.Password) 
    </div> 
    <div class="editor-label"> 
     <span class="red">*</span>Confirm Password 
    </div> 
    <input id="confirm-password" class="editor-field" type="password" /> 
    <p> 
     <input class="button" style="margin-left: 55px" type="submit" value="Submit" /> 
    </p> 
</fieldset> 

視圖這些腳本,是呈現佈局:

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

我需要驗證密碼和確認密碼字段,電子郵件由MVC驗證。

感謝您的幫助

回答

0

如果您在類「RegistrationViewModel」添加的屬性,上面的JavaScript代碼是不必要的。

你最好回顧一下原生MVC示例,這對學習者來說確實是一個很大的寶貝。

2

你可以這樣做容易和簡單:

  • 在RegistrationViewModel,添加命名空間System.ComponentModel.DataAnnotations
  • 添加驗證屬性,密碼和confirmpassword

    [Required(ErrorMessage = "Password required")] 
    [StringLength(int.MaxValue, MinimumLength = 6, ErrorMessage = "Password is at least 6 characters.")] 
    public string Password { get; set; } 
    
    [CompareAttribute("Password", ErrorMessage = "Password doesn't match.")] 
    public string ConfirmPassowrd { get; set; } 
    
  • 而且在你剃鬚刀,使用簡單,無需添加jquery來處理confirmpassword

    <div class="editor-label"> 
    <span class="red">*</span>@Html.LabelFor(model => model.Password) 
    </div> 
    <div class="editor-field" id="password"> 
        @Html.EditorFor(model => model.Password) 
        @Html.ValidationMessageFor(model => model.Password) 
    </div> 
    <div class="editor-label"> 
    <span class="red">*</span>Confirm Password 
    </div> 
    <div class="editor-field" id="confirmPassword"> 
        @Html.EditorFor(model => model.ConfirmPassword) 
        @Html.ValidationMessageFor(model => model.ConfirmPassword) 
    </div> 
    

希望得到這個幫助!

+0

如果我的模型沒有ConfirmPassword屬性,那麼如何?我只需要比較Password和ConfirmPassword即可,只有密碼存儲在數據庫中。 –

+0

你好新學習者,ConfirmPassword只是一個比較屬性,並且不需要存儲。如果您不需要它,請在ConfirmPassword文本框上使用Jquery事件關鍵字或keydown。它比$(「CredentialForm」)。validate函數更有效。 –

+1

這個回答沒有幫助。 OP想知道他的代碼爲什麼不起作用,不是解決方法或更好的解決方案。 –