1

I am using the CompareAttribute in my ViewModel like so:

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

[Required]
[StringLength(100, ErrorMessage = "{0} should be atleast {2} characters long.", MinimumLength = 6)]
[DataType(DataType.Password)]
[Display(Name = "Comfirm password")]
[System.ComponentModel.DataAnnotations.Compare("Password", ErrorMessage = "The passwords provided should be the same")]
public string ConfirmPassword { get; set; }

Together with jquery.validate.unobtrusive.js this will trigger that my ComfirmPassword property is not the same as the Password property. But my values are the same!

I have been Googling like hell, and there is a lot of topics on this subject - but none of the "fixes" provided seems to work in my code.

The generated mark-up looks the following:

Password:

<input type="password" name="Password" id="Password" data-val-required="The field Password should be filled." data-val-length-min="6" data-val-length-max="100" data-val-length="Password should be atleast 6 charecters long." data-val="true" class="form-control">

ConfirmPassword:

<input type="password" name="ConfirmPassword" id="ConfirmPassword" data-val-required="The field Confirm password should be filled" data-val-length-min="6" data-val-length-max="100" data-val-length="Password should be atleast 6 charecters long." data-val-equalto-other="*.Password" data-val-equalto="'Confirm password' and 'Password' does not match." data-val="true" class="form-control">

I have noticed in the ConfirmPassword property mark-up one of its data-attributes is: data-val-equalto-other="*.Password". Can this be somewhat wrong?

janhartmann
  • 14,713
  • 15
  • 82
  • 138
  • `Can this be somewhat wrong?` - No, it's correct. What is your question? – Zabavsky Dec 29 '13 at 13:50
  • The problem is that it trigger that the passwords are not the same - while they are. :-) – janhartmann Dec 29 '13 at 14:05
  • Is this true only for the client side validation? – Zabavsky Dec 29 '13 at 14:32
  • I am totally unable to reproduce this. Could you post here some repro steps? Because I can post a step by step, starting from scratch, example that works just fine. So I guess there's something else that's causing the problem. Unfortunately in your question there's not enough details so that we are able to reproduce the issue you describe. – Darin Dimitrov Dec 29 '13 at 14:36
  • Yes, it only happens with the client side. if I disable the client side and post the form, its accepted just fine. – janhartmann Dec 29 '13 at 14:37
  • If seen some related issues: http://stackoverflow.com/questions/8376322/mvc3-compareattribute-client-side-bug – janhartmann Dec 29 '13 at 14:37
  • 4
    It appears that [System.ComponentModel.DataAnnotations.CompareAttribute](http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.compareattribute(v=vs.110).aspx) doesn't implement `IClientValidatable`, while [System.Web.Mvc.CompareAttribute](http://msdn.microsoft.com/en-us/library/system.web.mvc.compareattribute(v=vs.118).aspx) does. You can use the second one or implement own client validation. – Zabavsky Dec 29 '13 at 14:44
  • Are you maybe using an Ajax form or Html form? Is your form on the partial view? – Marko Dec 29 '13 at 14:44
  • @Zabavsky: I tried that and same problem. I found this tread (and answer), so i mock around a bit more and see if I can get it to work as he can. http://stackoverflow.com/a/20294040/99395 – janhartmann Dec 29 '13 at 14:58
  • @Marko I am having a similar problem and am using an Ajax form/partial view. Is there something you are aware of that might be causing this? – akousmata Nov 12 '14 at 00:33
  • 1
    @akousmata I know that client side unobtrusive validation does not work on the dynamically rendered form - if your form is in a partial view and being loaded via ajax you need to look at this question http://stackoverflow.com/questions/18598830/mvc-4-client-side-validation-not-working-for-the-form-which-is-loaded-using-ajax/18600489#18600489 – Marko Nov 12 '14 at 14:59

0 Answers0