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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T00:20:36+00:00 2026-05-16T00:20:36+00:00

I am trying to make a validator that will make sure that at least

  • 0

I am trying to make a validator that will make sure that at least 2 items are selected. The validator works correctly on the server side but the client side code never gets executed.

Here is the code:

 Sys.Mvc.ValidatorRegistry.validators["country"] = function (rule) {

        var min = rule.ValidationParameters["min"];        

        return function (value, context) {

            if (value >= min) return true;

            return rule.ErrorMessage;

        };
    };  

And here is the validator code:

  public class CountryValidator : DataAnnotationsModelValidator<CustomValidations.CountryAttribute>
    {
        private int _minimum;
        private string _message; 

        public CountryValidator(ModelMetadata metadata, ControllerContext context, CustomValidations.CountryAttribute attribute) : base(metadata,context,attribute)
        {
            _minimum = attribute.Minimum;
            _message = attribute.ErrorMessage; 
        }

        public override IEnumerable<ModelClientValidationRule> GetClientValidationRules()
        {
            var rule = new ModelClientValidationRule()
            {
                ErrorMessage = _message,
                ValidationType = "country"
            };

            rule.ValidationParameters.Add("min", _minimum);
            return new[] { rule };
        }


    }

I have even registered the validation adapter in global.asax file:

  protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            RegisterRoutes(RouteTable.Routes);

            DataAnnotationsModelValidatorProvider.RegisterAdapter(typeof(AgeAttribute), typeof(AgeValidator));
            DataAnnotationsModelValidatorProvider.RegisterAdapter(typeof(CountryAttribute),typeof(CountryValidator));
        }

I am thinking that the validator only works with the elements that have a value property like textboxes etc.

UPDATE 1:

EnableClientValidation is invoked correctly and all the required JS files are included in the project. It seems like I need to attach the onblur to the context. I will try that and post the results.

  • 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-16T00:20:36+00:00Added an answer on May 16, 2026 at 12:20 am

    I think it is because the default validation is invoked on the onblur event of the input textbox. And for a listbox this event was not being thrown.

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

Sidebar

Related Questions

I am trying to create an form validator class that will check to make
I am trying to create a validation that checks to make sure a domain/url
I am trying to make a validation that will validate that the entered date
I'm trying to make a custom FxCop rule that will test for calls to
I'm trying to make a fork of MongoEngine that will allow auto updating of
Have have been trying to make a validator for my xml files. I have
I am trying to make my site fully w3c validator compliant. At the moment,
I'm trying make an entity with doctrine that has three associations with other entities
I am trying to make a view that allows a user to edit a
I have been trying to make sense of some claims that Appcelerator Titanium makes.

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.