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 655693
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T22:37:57+00:00 2026-05-13T22:37:57+00:00

I’ve read Phil Haack’s post on custom client-side validation in ASP.NET MVC 2. I

  • 0

I’ve read Phil Haack’s post on custom client-side validation in ASP.NET MVC 2. I want to do the same thing but with the jQuery adapter and using ASP.NET MVC 2 RC (as opposed to MVC 2 Beta that the post uses). Has anyone been able to figure how to do this?

I specially want to implement the password matching validation (i.e. password & confirm password must match). The ASP.NET MVC 2 RC VS.NET project template does show how to implement that on the server-side (using the PropertiesMustMatchAttribute) but not on the client-side.

  • 1 1 Answer
  • 0 Views
  • 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-13T22:37:57+00:00Added an answer on May 13, 2026 at 10:37 pm

    I assume you already followed Phil Haack’s instructions here http://haacked.com/archive/2009/11/19/aspnetmvc2-custom-validation.aspx) on how to get custom validation working with MS AJAX client validation. To get it to work with jQuery, you’ll need to modify the MicrosoftMvcJQueryValidation.js file:

    • In the __MVC_CreateRulesForField(validationField) function, you’ll need to add a case statement. Continuing Phil’s example, you’ll need to add:

      case “price”:

      __MVC_ApplyValidator_Price(rulesObj, thisRule.ValidationParameters[“min”]);

      break;

    • You’ll then need to create the __MVC_ApplyValidator_Price function:

    function __MVC_ApplyValidator_Price(object, value) {

    // min is what jQuery Validate uses to validate for minimum values
    object["min"] = value;
    

    }

    That should be enough to get Phil’s example working.

    Now, regarding your PropertiesMustMatchAttribute validation, it doesn’t look like MVC generates the client-side json validation definition for attributes that decorate classes. Since PropertiesMustMatchAttribute must be used on the model (and not the property), I can’t figure out how to make it trigger client-side validation. Instead, I took a different approach. I created a dummy validation attribute who’s IsValid() overload always returns true, and used this attribute on a property. This is just a dummy attribute that will delegate the validation logic to jQuery validator’s equalTo function. Here’s the dummy attribute:

    public class PropertiesMustMatchClientTriggerAttribute : ValidationAttribute
    {
        public string MatchProperty { get; set; }
    
        public PropertiesMustMatchClientTriggerAttribute(string matchProperty)
        {
            MatchProperty = matchProperty;
            ErrorMessage = "{0} doesn't match {1}.";
        }
        public override bool IsValid(object value)
        {
            return true;
        }
    
        public override string FormatErrorMessage(string name)
        {
            return String.Format(CultureInfo.CurrentCulture, ErrorMessageString, name, MatchProperty);
        }
    }
    

    Here is the custom validator:

    public class PropertiesMustMatchClientTriggerValidator : DataAnnotationsModelValidator<PropertiesMustMatchClientTriggerAttribute>
    {
        private string _message;
        private string _matchProperty;
    
        public PropertiesMustMatchClientTriggerValidator(ModelMetadata metaData, ControllerContext context, PropertiesMustMatchClientTriggerAttribute attribute)
            : base(metaData, context, attribute)
        {
            _message = attribute.FormatErrorMessage(metaData.DisplayName);
            _matchProperty = attribute.MatchProperty;
        }
    
        public override IEnumerable<ModelClientValidationRule> GetClientValidationRules()
        {
            var rule = new ModelClientValidationRule
            {
                ErrorMessage = _message,
                ValidationType = "equalTo"
            };
            rule.ValidationParameters.Add("matchField", _matchProperty);
    
            return new[] { rule };
        }
    }
    

    the above custom validator needs to be registered in Application_Start() per Phil’s blog:

    DataAnnotationsModelValidatorProvider.RegisterAdapter(typeof(PropertiesMustMatchClientTriggerAttribute), typeof(PropertiesMustMatchClientTriggerValidator));

    Finally, you need to modify the MicrosoftMvcJQueryValidation.js file:

    • Add the following case statement to __MVC_CreateRulesForField:

    case “equalTo”:

    __MVC_ApplyValidator_EqualTo(rulesObj, thisRule.ValidationParameters[“matchField”]);

    break;

    • add this function:

    function __MVC_ApplyValidator_EqualTo(object, elemId) {

    object["equalTo"] = document.getElementById(elemId);
    

    }

    Now you need to attach the dummy validation attribute to a property:

        [PropertiesMustMatchClientTrigger("Password")]
        public string ConfirmPassword { get; set; }
    

    That should do it.

    Creating this dummy attribute is a bit ugly, so I hope someone can come up with a more elegant solution.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 377k
  • Answers 377k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer It because you add a border on hover, and there… May 14, 2026 at 8:45 pm
  • Editorial Team
    Editorial Team added an answer > CONVERT() provides a way to convert data between different… May 14, 2026 at 8:45 pm
  • Editorial Team
    Editorial Team added an answer I strongly recommend StatSVN. It's a free, open-source tool. We've… May 14, 2026 at 8:45 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.