1

I have the following model

public AccountInfo {

    // loads of other property here as well.

    public string Password { get; set; }

    public string NewPassword { get; set; }

    [Compare("NewPassword", ErrorMessage = "New Passwords dont match.")]
    public string ConfirmNewPassword { get; set; }

}

I am using data annotations to display client side validation message.

Now I am working on the change user profile details page.

What I am required to do is, Along with the other details such as email, full name, address etc, I have to show 3 fields namely

  • Current password
  • New password
  • Confirm New password

Now the scene is that these are optional fields and user may not fill it. But when he does, I want to make sure all the 3 fields are filled., if not I want to show some validation error using data annotation.

Any Thoughts ?

Yasser Shaikh
  • 46,934
  • 46
  • 204
  • 281
  • There is a bunch of results regarding the conditional validation http://stackoverflow.com/questions/2417113/asp-net-mvc-conditional-validation http://blogs.msdn.com/b/stuartleeks/archive/2011/10/06/flexible-conditional-validation-with-asp-net-mvc-3 http://andrewtwest.com/2011/01/10/conditional-validation-with-data-annotations-in-asp-net-mvc/ – Jahan Zinedine Dec 03 '12 at 06:25
  • this solution does not work with client side validation – Yasser Shaikh Dec 03 '12 at 06:30
  • Check this link, http://blogs.msdn.com/b/simonince/archive/2011/02/04/conditional-validation-in-asp-net-mvc-3.aspx – Jahan Zinedine Dec 03 '12 at 06:31
  • It's not only server side, didn't you see the javascript code snippets which is for unontrusive validation on client side? – Jahan Zinedine Dec 03 '12 at 07:11
  • but that's not using data annotation right ? I wanted to do this using data annotations – Yasser Shaikh Dec 03 '12 at 07:31
  • As I read he's developed a custom annotation – Jahan Zinedine Dec 03 '12 at 08:34

2 Answers2

1

There isn't any built in data annotation for this. You could create your custom attribute to do the validation, but it's not easy. As such, I would suggest you to use Jquery Validation / Javascript to handle this.

Basically you would want to override the submit event and do your own validation logic in there. You code will be similar to this:

function SubmitToServer() { 
    if ($('#Password').length == 0 || ($('#Password').length > 0 && $('#NewPassword ').length > 1 && $(formId).valid()) {
        $(formId).submit();
    }
}
Jack
  • 2,600
  • 23
  • 29
0

You can use [Required] to tell the RAZOR that these are must or mandatory like that..

[Required]
public string Password { get; set; }

 [Required]
 public string NewPassword { get; set; }

 [Required]
 [Compare("NewPassword", ErrorMessage = "New Passwords dont match.")]
 public string ConfirmNewPassword { get; set; }

If you want to Provide range of value means use this one - [Range(1, 100)]

If you want to use password lenght means use this one - [StringLength(5)]

Yasser Shaikh
  • 46,934
  • 46
  • 204
  • 281
Harish
  • 1
  • 1
    thanks for your answer, BUT this is not what I am looking for. please read the question again. I want to make sure all the 3 fields are filled if even one of them is filled and they can all be empty also. – Yasser Shaikh Dec 03 '12 at 06:50