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

The Archive Base Latest Questions

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

I need to create a user control with form fields for Country (dropdown), Address

  • 0

I need to create a user control with form fields for Country (dropdown), Address (text box), City (text box), State (dropdown), and Zip (text box). I only want to validate City, State, and Zip if “United States” is chosen from the dropdown list. By “validate” I mean check for length – that’s it.

I’ve tried using custom validators but I’m missing something because code that seemingly should work isn’t doing anything. Example:

<asp:CustomValidator ErrorMessage="City, State, and Zip are required fields" 
Display="None" ID="LocationValidator" 
runat="server" ClientValidationFunction="validateLocation" 
onservervalidate="LocationValidator_ServerValidate">
</asp:CustomValidator>

Then here is my validation code

Client-side Validation:

function validateLocation(sender, args) {
    var country = jQuery("#main_2_MailingAddress_Country").val();
    var city = jQuery("#main_2_MailingAddress_City").val();
    if (city.Length() > 0)
    {
        args.IsValid = true;
    }
    else
    {
        args.IsValid = country != "United States";
    }
}

Server-side Validation:

protected void LocationValidator_ServerValidate(object source, ServerValidateEventArgs args)
    {
        if (City.Text.Length > 0)
        {
            args.IsValid = true;
        }
        else //nothing was entered for "City"
        {
            args.IsValid = Country.SelectedValue != "United States";
        }
        //similar functions for State and Zip go here
    }

If I only do server-side validation and don’t fill anything in on the form (there are other required fields besides the address fields) then the RequiredFieldValidators I have for those other fields fire. However if I fill in all fields except for City, State, and Zip (choosing U.S. for the country) then the form submits without catching that those are empty.

If I specify both client and server-side validation for my custom validator (as in the above example), none of the validators are fired and the form submits.

I realize that this may be a long and confusing post, but are there any ideas on where I’m going wrong?

  • 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-20T01:16:10+00:00Added an answer on May 20, 2026 at 1:16 am

    It’s hard to say what went wrong with the RequiredFieldValidators as they’re not in the example (did you specify which control to validate?), but here’s an example that should hopefully help you:

    protected void LocationValidator_ServerValidate(object source, ServerValidateEventArgs args)
            {
                args.IsValid = IsCountryValid();
            }
    
            private bool IsCountryValid()
            {
                if (Country.SelectedValue == "US")
                {
                    if (String.IsNullOrEmpty(City.Text))
                        return false;
    
                    if (String.IsNullOrEmpty(State.SelectedValue))
                        return false;
    
                    if (String.IsNullOrEmpty(Zip.Text))
                        return false;
                }
                else if (String.IsNullOrEmpty(Country.SelectedValue))
                {
                    return false;
                }
    
                return true;
            }
    

    I changed the display to ‘dynamic’ in order to be able to see the message:

    <asp:CustomValidator ID="LocationValidator" runat="server" 
                ErrorMessage="City, State, and Zip are required fields" Display="Dynamic" 
                OnServerValidate="LocationValidator_ServerValidate"></asp:CustomValidator>
    

    If you have values instead of empty strings in the dropdown then obviously change the validation for those, but I hope this helps!

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

Sidebar

Related Questions

I need to create a user control, that will be used in an application
I need to create a Drop Down List in a Web User Control (.NET
I need to create a text area wherein the user can adjust the font,
I need to create the progress bar on a user form. For inserting progress
Windows Form (in C#) - I need to create a custom listbox control for
Sometimes in Salesforce tests you need to create User objects to run part of
I need to create a user configurable web spider/crawler, and I'm thinking about using
I need to create a custom user interface using extJS/JSP which will allow me
I need to create an ejabberd user from a PHP script. I also need
For a large web application I'm building, I need to create an extensive user

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.