---
title: "ASP.NET MVC 5 Validation - Property are different"  
description: "ASP.NET MVC 5 Validation - Property are different"  
author: "Anonymous User"  
published: 2014-12-26  
updated: 2014-12-26  
canonical: https://www.mindstick.com/forum/12813/asp-dot-net-mvc-5-validation-property-are-different  
category: ".net"  
tags: ["mvc5", "validation"]  
reading_time: 2 minutes  

---

# ASP.NET MVC 5 Validation - Property are different

Hi I thought this was a simple one.To reduce the problem for the sake of clarity I have a three [properties](https://www.mindstick.com/articles/23331/nootropics-7-different-types-and-their-unique-properties) in my [view model](https://www.mindstick.com/forum/160237/explain-razor-view-model-binding-how-does-it-work-and-why-is-it-important), [user name](https://www.mindstick.com/interview/33836/how-to-check-current-user-name-of-database-in-sql-server), password and [confirm password](https://www.mindstick.com/blog/117/how-we-create-confirm-password-page-in-asp-dot-net).

Obviously we want to make sure the [passwords](https://www.mindstick.com/blog/301672/how-time-based-one-time-passwords-work) match but I also want to make sure that they do not match the [username](https://www.mindstick.com/forum/12798/there-is-no-viewdata-item-of-type-ienumerable-selectlistitem-that-has-the-key-username).

There are lots of hacks out there for [previous](https://yourviews.mindstick.com/view/81421/unlock-2-0-is-just-like-previous-corona-lockdowns) [version](https://www.mindstick.com/articles/12845/things-to-remember-while-migrating-odoo-to-a-better-version) of MVC but I can't believe there is not a more subtle way of dealing with this obvious [comparison](https://www.mindstick.com/articles/23182/comparison-maruti-suzuki-celerio-v-s-tata-tiago) in MVC5

```
[Required]    [DataType(DataType.Text)]    [Display(Name = "User name")]    public string UserName { get; set; }     [Required]    [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]    [DataType(DataType.Password)]    [Display(Name = "Password")]    [NotEqualTo("UserName", ErrorMessage = "The password can not be the same as the User name.")]    public string Password { get; set; }     [Required]    [DataType(DataType.Password)]    [Display(Name = "Confirm password")]    [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]    public string ConfirmPassword { get; set; }
```

## Replies

### Reply by Manoj Bhatt

I'm a big fan of using the FluentValidation library when dealing with any non-standard validations. It even contains a NotEqual() method for doing exactly what you're talking about.

```
public class RegisterViewModelValidator: AbstractValidator<RegisterViewModel>  {    public RegisterViewModelValidator()    {        RuleFor(m =>m.Password).NotEqual(m => m.UserName).WithMessage("Password Cannot
Match Username");    }}
```

You may want to go a step further and change it not allow the [password](https://www.mindstick.com/blog/118/retriving-user-password-by-using-stored-procedure-in-asp-dot-net) to CONTAIN the username.

```
RuleFor(m => m.Password).Must((vm, pwd) => !pwd.Contains(vm.UserName)).WithMessage("Password
Cannot Match Username");
```

While you can mix FluentValidation and the standard attributes as well, MVC will get mad if they both apply the same validation (for example, including the [Required] attribute and also adding the NotEmpty() rule in Fluent will throw an error).


---

Original Source: https://www.mindstick.com/forum/12813/asp-dot-net-mvc-5-validation-property-are-different

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
