ASP.NET comes in with a set of validation controls that automatically validate the data entered by a user. Though this validation controls are very powerful and easy to use, they have a small draw back in the sense that they require the entire page to be valid before it's submitted back to the server. There is no direct way to validate only a particular section of the page. This article will explain some circumstances where validating a particular section of page will be required and how to accomplish it.
While validation controls is a great tool for validating user input before data is being submitted there are some situations where we might either want the data to be submitted without validation or we want to validate only particular fields in the form.
A typical example for when we may want the data to be passed through without validation is when a page has both submit and cancels server buttons. When the user clicks the submit button the data has to be validated whereas when the user clicks the cancel post back has to happen without any validation.
The solution for by passing validation on server button click is fairly easy. All we have to do is set the CausesValidation property of the button control to false. Once the CausesValidation property is set to false no validation will happen both on the client side and server side.
The syntax for doing it in the design time is
<asp:button id="cmdCancel" runat="server" Text="Cancel" CausesValidation="False"></asp:button>
This can also be done in runtime using the following code
cmdCancel.CausesValidation = false;
To disable a particular validation control we can use the ValidatorEnable function as shown in the script below
<script language="javascript">
ValidatorEnable(nameofvlaidationcontrol, false)
</script>
To disable all the validation control we need to loop through Page_Validators array and disable each validator as shown in script below
<script language="javascript">
for(i=0;i< Page_Validators.length;i++)
{ ValidatorEnable(Page_Validators[i], false);
}
</script>
In order to enable validation only for particular set of controls when a submit button is clicked we need to combine the above two scripts and call it from the client click event of the button as shown in sample below
<script language="javascript">
function enableValidators()
{
for(i=0;i< Page_Validators.length;i++)
{
ValidatorEnable(Page_Validators[i], false)
}
ValidatorEnable(rvState, true);
}
</script>
Attaching the function to the client click event of a submit button
cmdSave.Attributes.Add("onclick","enableValidators();");
When the cmdSave submit button is clicked all the other validators will be disabled and only the validator named rvState will be enabled. If the validation of rvState is successful then the page will be submitted. The code we have used so far will disable the validatiors only on the client side, so the validation will still happen on the server side and error messages will be shown. To disable particular validators on the server side the following code has to be added to the button click event
validationcontrolname.IsValid=true;