There are several situations that require you to disable one or more validators, or to request validation where it's not normally done:
- Some buttons do not save anything, such as a Cancel button. You need to prevent the button from validating.
- There are two or more buttons on the page and each has its own list of validators to fire.
- A validator should be evaluated based on some condition on the page, such as the value of another control.
- AutoPostBack should validate to avoid a post back if the page has errors.
Preventing a button from validating
Here is one of the most overlooked features of ASP.NET Validation. I see developers ask about this several times a week on the www.asp.net forums. Now you don't have to be one of them.
When you have a Cancel button or any other button that should never validate anything, simply set its CausesValidation property to false. The Button, LinkButton, ImageButton and the HtmlControl buttons all have this property.
<asp:Button id="CancelBtn" runat=server Text="Cancel" CausesValidation="false" />
Several buttons associated with their own validators
Suppose you have a login or search section on a page that also has some data entry fields. Each section has its own submit button with validators. When you setup validators on all the fields and click one submit button, the validators from the other section report errors, preventing the form from submitting.
Microsoft recognized that ASP.NET needs to handle this case has introduced Validation Groups in ASP.NET 2.0. (My Professional Validation And More already has Validation Groups.) To use validation groups, create a group name for each section. Then assign that name to the ValidationGroup property found on ASP.NET 2.0 buttons and validators in that section. It's very simple.
What happens if you have ASP.NET 1.x? You will have to use server-side validation when the page is submitted to validate the individual validators associated with the submit button:
- Set the submit control's CausesValidation property to false.
- In the Click event method for each submit button, call the Validate() method on each validator that is in the section. Then test that the IsValid property of each validator in the section is true. For example:
[C#]
RequiredFieldValidator1.Validate();
RequiredFieldValidator2.Validate();
if (RequiredFieldValidator1.IsValid && RequiredFieldValidator2.IsValid)
{
// your code here
}
[VB]
RequiredFieldValidator1.Validate()
RequiredFieldValidator2.Validate()
If RequiredFieldValidator1.IsValid And RequiredFieldValidator2.IsValid Then
' your code here
End If
Now this design prevents client-side validation from occuring when the user clicks submit. (Validation will still occur as the user changes individual fields.) Some users have attempted to create client-side code that runs when the user clicks on the submit button and sets each validator’s enabled attribute to false or true. That way, the client-side validation will still run, but will skip the validators that are not enabled. This is a bad design as it leaves those validators disabled. If there was an error, the user would continue to edit the form with some of the validators now disabled.
Enabling validators
Microsoft designed ASP.NET validators so you can write server-side code to solve the shortcomings of client-side code. One of those shortcomings is a very common situation: you have a validator that should not validate unless there is a specific condition on the page. Examples include a checkbox is marked, another textbox has data, and the data entry field is enabled. In my product, I solved this by providing each validator with a property called Enabler where you specify the rule that enables the validator.
When using Microsoft's validators, there are several ways to do this.
1. Use server-side validation. Call individual validator's Validate() method in the Click event method after determining if the validator should fire.
a. Turn off client-side validation on each validator that needs to be disabled by setting EnableClientScript to false.
b. Set CausesValidation to false on the submit button.
c. In the Click event method, call the Validate() method on each validator but add logic to skip any validator that should not validate. Then test the IsValid property on each validator for true before saving.
Here is an example where RequiredFieldValidator2 should be enabled only when CheckBox1 is checked. RequiredFieldValidator1 will always validate:
[C#]
RequiredFieldValidator1.Validate();
if (CheckBox1.Checked)
RequiredFieldValidator2.Validate();
bool vIsValid = true;
if (!RequiredFieldValidator1.IsValid)
vIsValid = false;
if (vIsValid && CheckBox1.Checked && !RequiredFieldValidator2.IsValid)
vIsValid = false;
if (vIsValid)
{
// your code here
}
[VB]
RequiredFieldValidator1.Validate()
If CheckBox1.Checked Then
RequiredFieldValidator2.Validate()
End If
Dim vIsValid As Boolean = True
If Not RequiredFieldValidator1.IsValid Then
vIsValid = False
End If
If vIsValid And CheckBox1.Checked And Not RequiredFieldValidator2.IsValid Then
vIsValid = False
End If
If vIsValid Then
' your code here
End If
2. Use a CustomValidator. This will let you preserve client-side validation by creating some client-side code for validation. The problem is that you end up writing the same logic already created in the existing validators. While developing the same logic as in the RequiredFieldValidator is easy (remember to trim spaces!), try writing a CompareValidator for dates. (As the author of a popular DatePicker control, I can assure you that date parsing in Javascript is not easy!)
There's one other challenge here: you will be writing JavaScript and using DHTML. If you lack experience in these technologies, this becomes a much bigger task.
See the .NET documentation topic, CustomValidator Overview, for details on using a CustomValidator. Be sure to write code for both client and server-side validation.
AutoPostBack and validation
In ASP.NET 1.x, the AutoPostBack features of data entry controls do not validate before submitting. If they did, the page could avoid an unnecessary trip to the server.
ASP.NET 2.0 introduces two new properties of data entry controls: CausesValidation and ValidationGroup. These are exclusively used when AutoPostBack is true. When CausesValidation is true, it will perform client-side validation using the validation group defined in the ValidationGroup property.
Be careful in using this feature because all validators of a Validation Group are evaluated. So any controls that have not been filled in yet will have their RequiredFieldValidator fired, preventing post back.