Mar
04

No Validation Messages when Disabling & Enabling ASP.Net Validator in Javascript

posted on 04 March 2010 in programming with 0 Comments

Warning: Please consider that this post is over 7 years old and the content may no longer be relevant.

Need to validate a form field based on the value of another field in ASP.Net? The .Net Framework provides a javascript function to do just that:

ValidatorEnable(val, enable)

This function takes the validator control (val) and a boolean to determine whether it should be enabled or disabled. The problem with this function is that it triggers the validator to validate itself immediately, showing any validation error messages before the user has submitted the form, which may not always be want you want.  The alternative is to set the validator’s enabled property to trigger this validator to validate itself when the user submits the form, and by using the Page_IsValid variable you can test whether the user has triggered a validation or not, then use ValidatorEnable function only after the validation has occurred when the user expects to see validation messages.

Consider a scenario where you want the user to enter their address only if they select Australia as the country, you also need to validate the address fields. Two ways to approach this are:

  • For each address field you need to validate use a CustomValidator control, and in the ClientValidationFunction first check to see if the country is set to Australia and if it’s not then consider any response for this control as valid. The problem with this is you will have to recrete all of the validator functionality in your client validation function, which means more work.
  • Use standard validator controls for the address fields, and enable or disable them in javascript.  The code you call to enable or disable the validators could be a CustomValidation control on the country field, or a standard javascript call when the user selects a country. If the Page_IsValid variable is set to true then assume that the page has just been loaded and no validation has occured, in this case just set the validator controls enabled property so it will be validated (or not) when the user submits the form.
    If Page_IsValid is false then assume the user has tried to submit but failed, in this case we expect validation messages to show / hide as the user changes data on the form - this is the normal ASP.Net behaviour.

Here’s an example:

// The ClientValidationFunction of a custom validator for country
function validateCountry(source, args) {
    // We we haven't submitted the form yet
    if (Page_IsValid) {
        // Turn the postcode validator on / off, but don't validate
        requiredFieldValidatorPostcode.enabled = enable;
    } else {
        // Turn the postcode validator on / off, and immediately validate
        ValidatorEnable(requiredFieldValidatorPostcode, enable);
    }

    // Validate country is not empty
    args.IsValid = (args.Value.length > 0);
}

Don’t forget to include server side code to mimic this enabling / disabling of validators. To do this override the Validate() function, enable or disable validators depending on the country selected and call the base.Validate() function.

public override void Validate()
{
    // Check if we need to validate the postcode.
    bool requireAddress = (dropDownListCountry.Text == "Australia");
    requiredFieldValidatorPostcode.Enabled = requireAddress;

    // Call the normal validation methods.
    base.Validate();
}

protected void customValidatorCountry_ServerValidate(object source, ServerValidateEventArgs args)
{
    // Implement the server side custom validation for country.
    args.IsValid = (args.Value.Length > 0);
}