aspdotnet-suresh offers articles and tutorials,csharp dot net, articles and tutorials,VB.NET Articles,Gridview articles,code examples of 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

Asp.Net MVC Validation Compare Passwords Example

Nov 22, 2016

Here I will explain how to implement compare validator in mvc using DataAnnotations with example or mvc validation to compare two properties with examples or validation to compare passwords in mvc with example or Compare properties in model using DataAnnotations in mvc with example. In mvc we can easily implement compare validation on model properties by using DataAnnotations compare property.

Generally in mvc we can easily compare two properties by using DataAnnotations Compare property. We will see how to implement compare validation with example for that create mvc application and Open visual studio à Go to File à Select New à Project like as shown below.

Once we select Project new popup will open in that select Asp.Net Web Application and give name to application and click OK like as shown below

Once click OK new popup will open in that select Empty template and select folders and core reference as MVC and click OK like as shown below

Once we finished creating application our project structure will be like as shown below

Now right click on your Models folder à select Add à select Class like as shown below.

Once we add new model (UsersModel) open it and write code like as shown below.

using System.ComponentModel.DataAnnotations;

namespace MVCExamples.Models
public class UsersModel
public int UserId { get; set; }
public string UserName { get; set; }
public string Password { get; set; }
[Compare("Password",ErrorMessage= "Both Password and Confirm Password Must be Same" )]
public string ConfirmPassword { get; set; }
public string Location { get; set; }

If you observe above code we added DataAnnotations reference and used Compare property to compare Password and Confirm Password properties.

Now add new controller for that right click on your Controllers folder à select Add à select Controller like as shown below.

Once we click on Controller new popup will open in that select MVC 5 Controller - Empty and click Add like as shown below.

Once click on Add new window will open in that give name of controller and click Add button then new controller file will add to folder. Now open new controller (UserController) and write the code like as shown below

using System.Web.Mvc;

namespace MVCExamples.Controllers
public class UserController : Controller
// GET: User
public ActionResult Index()
return View();
public ActionResult UserRegistration()
return View();

Now right click on UserRegistration method and select Add View like as shown below

Once click Add View new template will open in that select Template type “Create” and Model class as our “UsersModel” and click Add like as shown below.

Now run your application and check the output that will be like as shown below

If you enjoyed this post, please support the blog below. It's FREE!

Get the latest,, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email


Unknown said...

Hi Suresh Sir,
I have requirement like change password in my application (mvc 5 ) can you please help on this.

Give your Valuable Comments

Other Related Posts

© 2015 All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.