Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 661755
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T23:18:09+00:00 2026-05-13T23:18:09+00:00

I’m just getting started with ASP.NET MVC 2, and playing around with Validation. Let’s

  • 0

I’m just getting started with ASP.NET MVC 2, and playing around with Validation.

Let’s say I have 2 properties:

  • Password1
  • Password2

And I want to require that they are both filled in, and require that both are the same before the model is valid.

I have a simple class called “NewUser”.

How would I implement that? I’ve read about ValidationAttribute, and understand that. But I don’t see how I would use that to implement a validation that compares two or more properties against eathother.

Thanks in advance!

Problem with below solution:

When this is applied to an application, and the ModelBinder runs the validation of the Model, then there is a problem:

If a Property-level ValidationAttribute contains an error, then the Class-level ValidationAttribute‘s are NOT validated. I have not found a solution to this problem as of yet.

If you have a solution to this problem please share your experience. Thanks alot!

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T23:18:09+00:00Added an answer on May 13, 2026 at 11:18 pm

    The default ASP.NET MVC 2 template of Visual Studio includes the exact validation attribute you need. Pasted from AccountModels.cs file :

    [AttributeUsage(AttributeTargets.Class, 
        AllowMultiple = true, Inherited = true)]
    public sealed class PropertiesMustMatchAttribute : ValidationAttribute {
        private const string _defaultErrorMessage = 
            "'{0}' and '{1}' do not match.";
        private readonly object _typeId = new object();
    
        public PropertiesMustMatchAttribute(string originalProperty, 
            string confirmProperty)
            : base(_defaultErrorMessage) {
            OriginalProperty = originalProperty;
            ConfirmProperty = confirmProperty;
        }
    
        public string ConfirmProperty { get; private set; }
        public string OriginalProperty { get; private set; }
    
        public override object TypeId {
            get {
                return _typeId;
            }
        }
    
        public override string FormatErrorMessage(string name) {
            return String.Format(CultureInfo.CurrentUICulture, ErrorMessageString,
                OriginalProperty, ConfirmProperty);
        }
    
        public override bool IsValid(object value) {
            PropertyDescriptorCollection properties = 
               TypeDescriptor.GetProperties(value);
            object originalValue = properties.Find(OriginalProperty, 
                true /* ignoreCase */).GetValue(value);
            object confirmValue = properties.Find(ConfirmProperty, 
                true /* ignoreCase */).GetValue(value);
            return Object.Equals(originalValue, confirmValue);
        }
    }
    

    How to use :

    [PropertiesMustMatch("Password", "ConfirmPassword", 
        ErrorMessage = "The password and confirmation password do not match.")]
    class NewUser {
        [Required]
        [DataType(DataType.Password)]
        [DisplayName("Password")]
        public string Password { get; set; }
        [Required]
        [DataType(DataType.Password)]
        [DisplayName("Confirm password")]
        public string ConfirmPassword { get; set; }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.