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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T03:59:57+00:00 2026-06-11T03:59:57+00:00

Need some advice I am trying to write a validator which only fires when

  • 0

Need some advice I am trying to write a validator which only fires when a specific value in a dropdownlist is selected.

I have two drop down lists on this form one for Countries and the other for US States, the States dropdownlist only shows when the United States is selected from the Countries dropdownlist.

I need a validator that makes the State dropdownlist list a required field only if the United States is selected as a country.

As background information this a MVC3 Web Application and the show/hide code for the States dropdownlist is JQuery.

  • 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-06-11T03:59:59+00:00Added an answer on June 11, 2026 at 3:59 am

    Another alternative is to dynamically add the rule to jQuery for validation.
    You would need to check on the server side this custom logic as well however.
    You can do this in your controller, or ideally your VieWModel would implement IValidateableObject to check if country=”usa” then county is required.

    Use jQuery’s .rules.add and .remove:

    http://docs.jquery.com/Plugins/Validation/rules#.22remove.22rules

    So you could do something along the lines of:

    
    $(document).ready(function() {
        $("#country").change(function(){
            if($(this).val()=="usa")
            {
              $("#yourCountyDropDown").rules("add", {
               required: true,
               messages: {
                 required: "County is required"
               }
              });
            }
            else
            {
              $("#yourCountyDropDown").rules("remove");
            }
        });
    });
    
    

    and for your ViewModel

    
    public class WhateverYourObjectNameCreateViewModel : IValidatableObject
    {
           #region Validation
            public IEnumerable Validate(ValidationContext validationContext)
            {
                if (this.Country=="USA" && string.IsNullOrEmpty(this.County))
                {
                    yield return new ValidationResult("County is required");
                }
            }
            #endregion
    }
    
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need some advice for a design which I have to implement in application.
I need some advice. I'm trying to use oRTP , which is a C
Need some quick advice I am trying to access a object array but I
I need some advice regarding which database I should use. I want to create
I need some advice of how to setup my tables I currently have a
I need some advice on which Swing Components to choose in order to achieve
I need some advice on how to go about a particular issue I'm trying
I need some advice with the Java Print API. I have a program that
I need some advice and guide line. I am trying to upload image into
I am building an application at work and need some advice. I have a

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.