2011-08-17 81 views
5

以下是原因。這是生成的HTML:即使密碼相同,比較驗證器也始終關閉

<div> 
     <label for="RegisterModel_Password">Contrase&#241;a</label> 
     <input class="text-box single-line password" data-val="true" data-val-length="Su Contrase&amp;#241;a debe tener al menos 6 caracteres." data-val-length-max="100" data-val-length-min="6" data-val-required="Debe escribir su contrase&amp;#241;a" id="RegisterModel_Password" name="RegisterModel.Password" type="password" value="" /> 
     <span class="field-validation-valid" data-valmsg-for="RegisterModel.Password" data-valmsg-replace="true"></span> 

    </div> 

    <div> 
     <label for="RegisterModel_ConfirmPassword">Confirme Su Contrase&#241;a</label> 
     <input class="text-box single-line password" data-val="true" data-val-equalto="Sus contrase&amp;#241;as no son las mismas." data-val-equalto-other="*.Password" id="RegisterModel_ConfirmPassword" name="RegisterModel.ConfirmPassword" type="password" value="" /> 
     <span class="field-validation-valid" data-valmsg-for="RegisterModel.ConfirmPassword" data-valmsg-replace="true"></span> 
    </div> 

注意到這一點,在確認密碼框中:

data-val-equalto-other="*.Password" 

這應該是RegisterModel.Password,因爲我猜的JavaScript看起來像一個名爲「RegisterModel輸入.Password「,不是?

這裏是我的模型代碼:

[Required(ErrorMessage = "Debe escribir su contraseña")] 
[StringLength(100, ErrorMessage = "Su {0} debe tener al menos {2} caracteres.", MinimumLength = 6)] 
[DataType(DataType.Password)] 
[Display(Name = "Contraseña")] 
public string Password { get; set; } 

[DataType(DataType.Password)] 
[Display(Name = "Confirme Su Contraseña")] 
[Compare("Password", ErrorMessage = "Sus contraseñas no son las mismas.")] 
public string ConfirmPassword { get; set; } 

上爲什麼發生這種情況的任何想法?


剛剛創建了一個全新的應用MVC3,這是模型:

[Required] 
[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)] 
[DataType(DataType.Password)] 
[Display(Name = "Password")] 
public string Password { get; set; } 

[DataType(DataType.Password)] 
[Display(Name = "Confirm password")] 
[Compare("Password", ErrorMessage = "The password and confirmation password do not match.")] 
public string ConfirmPassword { get; set; } 

和驗證工作。這裏唯一的區別是它們直接在視圖中使用模型,而我傳遞一個* 查看 *包含該模型的RegisterModel。

這個工作默認的HTML是不同的,雖然到我的:

 <div class="editor-field"> 
      <input data-val="true" data-val-length="The Password must be at least 6 characters long." data-val-length-max="100" data-val-length-min="6" data-val-required="The Password field is required." id="Password" name="Password" type="password" /> 
      <span class="field-validation-valid" data-valmsg-for="Password" data-valmsg-replace="true"></span> 
     </div> 

     <div class="editor-label"> 
      <label for="ConfirmPassword">Confirm password</label> 
     </div> 
     <div class="editor-field"> 
      <input data-val="true" data-val-equalto="The password and confirmation password do not match." data-val-equalto-other="*.Password" id="ConfirmPassword" name="ConfirmPassword" type="password" /> 
      <span class="field-validation-valid" data-valmsg-for="ConfirmPassword" data-valmsg-replace="true"></span> 
     </div> 
+0

只是確認這由什麼原因引起我懷疑。我把所有東西都移到了一個模型中,而不是ViewModel(正如我應該正確的那樣),jQuery驗證庫確實能夠正常工作。我不知道這是否是一個錯誤? –

回答

2

您好,我有同樣的問題比你。發生這種情況是因爲執行驗證的JavaScript文件存在錯誤。這是你必須解決的問題。

http://forums.asp.net/t/1716181.aspx/1

這並不是說很難在最小化的版本,這是我不得不改變什麼發現。

Saludos!

0

我有同樣的問題,上面的答案並沒有解決它不幸的。解決方法是確保我將[Required]和[StringLength(.....] Atrributes同時應用於密碼和確認密碼字段。我還必須將Compare屬性應用於密碼字段,而不是ConfirmPassword場。

因此,與上面的模型,

[Required(ErrorMessage = "Debe escribir su contraseña")] 
[StringLength(100, ErrorMessage = "Su {0} debe tener al menos {2} caracteres.", MinimumLength = 6)] 
[DataType(DataType.Password)] 
[Display(Name = "Contraseña")] 
public string Password { get; set; } 

[DataType(DataType.Password)] 
[Display(Name = "Confirme Su Contraseña")] 
[Compare("Password", ErrorMessage = "Sus contraseñas no son las mismas.")] 
public string ConfirmPassword { get; set; } 

變爲

[Required(ErrorMessage = "Debe escribir su contraseña")] 
[StringLength(100, ErrorMessage = "Su {0} debe tener al menos {2} caracteres.", MinimumLength = 6)] 
[DataType(DataType.Password)] 
[Display(Name = "Contraseña")] 
[Compare("ConfirmPassword", ErrorMessage = "Sus contraseñas no son las mismas.")] 
public string Password { get; set; } 

[Required(ErrorMessage = "Debe escribir su contraseña")] 
[StringLength(100, ErrorMessage = "Su {0} debe tener al menos {2} caracteres.", MinimumLength = 6)] 
[DataType(DataType.Password)] 
[Display(Name = "Confirme Su Contraseña")] 
public string ConfirmPassword { get; set; } 
0

而不是使用

@Html.PasswordFor(m => m.RegisterModel.RegisterConfirmPassword) 

其輸出

<input data-val="true" data-val-equalto="The password and confirmation password do not match." data-val-equalto-other="RegisterModel.RegisterPassword" id="RegisterModel_RegisterConfirmPassword" name="RegisterModel.RegisterConfirmPassword" type="password"> 

我把上面的輸出,並添加單引號括起來「‘RegisterModel.RegisterPassword’」

<input data-val="true" data-val-equalto="The password and confirmation password do not match." data-val-equalto-other="'RegisterModel.RegisterPassword'" id="RegisterModel_RegisterConfirmPassword" name="RegisterModel.RegisterConfirmPassword" type="password"> 
相關問題