Disable client side validation for fields in Web Forms

Disabling validation for hidden fields with ASP.NET Web Forms is certainly not as easy as doing with with ASP.NET MVC as I have posted about before, but it is totally doable.

You will have to work with the Page.Validators object on the client side. It is a global variable call Page_Validators, it is an array that contains all your validators for the page.

Basically you can cycle through this array and disable validators based on the validators id, you use a javascript function called ValidatorEnable() to enable/disable validators as you hide and show sections of your form.

Here is a code sample:

for (i = 0; i < Page_Validators.length; i++) {
    if (Page_Validators[i].id === "<%= RequiredFieldValidatorOne.ClientID %>") {
        ValidatorEnable(Page_Validators[i], false);
    }
    if (Page_Validators[i].id === "<%= RequiredFieldValidatorTwo.ClientID %>") {
        ValidatorEnable(Page_Validators[i], true);
    }
}               

Hopefully this saves you some time down the line for your more complex ASP.NET Web Forms.

Advertisements

Published by

Alan Feekery

Developer, Gamer, Musician, Cyclist and big Motorsport fan... enjoys the odd cup of coffee :)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s