Validation Controls in ASP.NET
Introduction
ASP.NET 2.0 offers a wide variety of ways to capture user input. This translates to, many opportunities for users to provide bad data. In this article, you will learn about the support that ASP.NET provides to you for performing data validation.
Validating Data
Often, bad data doesn't affect your organization until someone tries to report against it or some business intelligence project uncovers the presence of inconsistencies. If bad data is discovered in these circumstances, correcting it is an overwhelming, if not impossible, task. There could be millions of records with inconsistent and erroneous information. All these instances will skew your reports and make your business intelligence efforts more ambiguous.
So that you can avoid correcting bad data, you must validate data at the time of entry and, thereby, prevent bad data from getting into your systems. ASP.NET 2.0 provides some excellent support for performing data validation. In this article, you will see, choosing an appropriate data validation technique and then how to implement that technique using the intrinsic ASP.NET 2.0 data validation controls.
Appropriate Data Validation Technique
In a multi-tier architecture, the input source is not restricted to the UI and presentation layers. Data can come from many different sources such as other internal systems, other external systems, and other components of the same system. In this situation, it is not sufficient to rely solely on the presentation layer for validation.
On the other hand, if your application receives its input exclusively from the UI, then you can rely solely on the UI to validate the input data. In this article, we will mainly focus on validating data provided via a Web-based UI.
Validation Controls Common Properties
It should be no surprise that ASP.NET provides a solid base of validation controls. You can use these flexible controls out of the box or build on them through inheritance to create your own validation controls.
All ASP.NET validation controls have some common properties with which you should be familiar. The following listing describes the common properties among validation controls.
ControlToValidate: This property is used to indicate which control the validation control is to validate.
Display: This property provides three possible values. Setting the property to None prevents the control from being displayed inline. Use this setting in conjunction with the ValidationSummary control. The Static setting renders the validation control inline with other controls. This means that blank space is occupied by the control when it is not showing a message. Use the Dynamic setting when you want to avoid the control occupying blank space.
EnableClientScript: Not every browser supports JavaScript, and even browsers supporting it might have it disabled. However, for those browsers that support JavaScript, the validation controls can perform client-side validation as well as server-side validation. Client-side validation does not require a postback for validation to occur, resulting in a richer user experience. Note that client-side validation should be used only to provide a richer user experience and not as a replacement for server-side validation.
Enabled: This property determines whether the validation control actually performs validation.
ErrorMessage: This property is generally associated with the ValidationSummary control.
ForeColor: Use this property to set the color of the inline error message when validation fails.
IsValid: This property is used to evaluate whether validation fails.
SetFocusOnError: Setting this property to True causes the focus to be set to the control identified in the ControlToValidate property if validation fails.
Text: The Enabled property determines whether the validation control actually performs validation. The Text property is generally associated with the inline message–the message displayed at the location of the validation control.
ValidationGroup: Validation groups are new to .NET 2.0. This property is used to associate the current validation control with a specific validation group.
Now that you have been introduced to the common attributes of validation controls, you can dig deeper into specific facets of each validation control. You can use validation controls in combination. For example, you can use RequiredFieldValidator and RegularExpressionValidator controls on the TextBox control for capturing an e-mail address.
ASP.NET 2.0 offers a wide variety of ways to capture user input. This translates to, many opportunities for users to provide bad data. In this article, you will learn about the support that ASP.NET provides to you for performing data validation.
Validating Data
Often, bad data doesn't affect your organization until someone tries to report against it or some business intelligence project uncovers the presence of inconsistencies. If bad data is discovered in these circumstances, correcting it is an overwhelming, if not impossible, task. There could be millions of records with inconsistent and erroneous information. All these instances will skew your reports and make your business intelligence efforts more ambiguous.
So that you can avoid correcting bad data, you must validate data at the time of entry and, thereby, prevent bad data from getting into your systems. ASP.NET 2.0 provides some excellent support for performing data validation. In this article, you will see, choosing an appropriate data validation technique and then how to implement that technique using the intrinsic ASP.NET 2.0 data validation controls.
Appropriate Data Validation Technique
In a multi-tier architecture, the input source is not restricted to the UI and presentation layers. Data can come from many different sources such as other internal systems, other external systems, and other components of the same system. In this situation, it is not sufficient to rely solely on the presentation layer for validation.
On the other hand, if your application receives its input exclusively from the UI, then you can rely solely on the UI to validate the input data. In this article, we will mainly focus on validating data provided via a Web-based UI.
Validation Controls Common Properties
It should be no surprise that ASP.NET provides a solid base of validation controls. You can use these flexible controls out of the box or build on them through inheritance to create your own validation controls.
All ASP.NET validation controls have some common properties with which you should be familiar. The following listing describes the common properties among validation controls.
ControlToValidate: This property is used to indicate which control the validation control is to validate.
Display: This property provides three possible values. Setting the property to None prevents the control from being displayed inline. Use this setting in conjunction with the ValidationSummary control. The Static setting renders the validation control inline with other controls. This means that blank space is occupied by the control when it is not showing a message. Use the Dynamic setting when you want to avoid the control occupying blank space.
EnableClientScript: Not every browser supports JavaScript, and even browsers supporting it might have it disabled. However, for those browsers that support JavaScript, the validation controls can perform client-side validation as well as server-side validation. Client-side validation does not require a postback for validation to occur, resulting in a richer user experience. Note that client-side validation should be used only to provide a richer user experience and not as a replacement for server-side validation.
Enabled: This property determines whether the validation control actually performs validation.
ErrorMessage: This property is generally associated with the ValidationSummary control.
ForeColor: Use this property to set the color of the inline error message when validation fails.
IsValid: This property is used to evaluate whether validation fails.
SetFocusOnError: Setting this property to True causes the focus to be set to the control identified in the ControlToValidate property if validation fails.
Text: The Enabled property determines whether the validation control actually performs validation. The Text property is generally associated with the inline message–the message displayed at the location of the validation control.
ValidationGroup: Validation groups are new to .NET 2.0. This property is used to associate the current validation control with a specific validation group.
Now that you have been introduced to the common attributes of validation controls, you can dig deeper into specific facets of each validation control. You can use validation controls in combination. For example, you can use RequiredFieldValidator and RegularExpressionValidator controls on the TextBox control for capturing an e-mail address.
Labels: Validation Controls

0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home