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

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I'm looking for suggestions for debugging... If you view this site in Firefox or
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti

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.