Only validate city, state, and zip if "United States" is selected as a country

2.6k views Asked by At

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

There are 1 answers

2
annelie On BEST ANSWER

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!