2

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
    You can force a page validation using Page.Validate() on postback. Jan 28, 2011 at 22:25

1 Answer 1

3

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!

2
  • awesome, that works. the other key was adding a validation group to the customvalidator. Then I did a Page.Validate("validationgroup") and an if (!IsValid) from inside the Page_Load of where I was calling this user control. I'll give you the answer though since you helped me out with the logic - thanks!
    – esvendsen
    Feb 3, 2011 at 16:15
  • Glad I could help! And thanks, this was my first accepted answer. :)
    – annelie
    Feb 4, 2011 at 19:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.