AspAlliance.com LogoASPAlliance: Articles, reviews, and samples for .NET Developers
URL:
http://aspalliance.com/articleViewer.aspx?aId=1554&pId=-1
Validation in ASP.NET 2.0
page
by Samer Salem
Feedback
Average Rating: 
Views (Total / Last 10 Days): 20604/ 49

Introduction

With the new advanced version of ASP.NET, enhancements have been done to the use of validations. One of which is that ASP.NET validations work on most browsers other than Internet Explorer. We will show in this article the six types of validations: RequiredFieldValidator, RangeValidator, RegularExpressionValidator, CompareValidator, CustomValidator, and ValidationSummary.

Why we use Validations?

Validations are used on the fields of a form in ASP.NET webpage. They are used to make restrictions when entering the fields such as making it required or formatting the data entered in the field by the user.

Validations Controls

We will show first the common properties of all the six validations and then the other properties for each validator.

Common Properties for All Validation Controls

ControlToValidate: You can set the name of the control you want to validate and the name of all controls will display in a list.

Display: The value is Static/Dynamic; it allows the text of the error message displayed to move with the design if it is set as dynamic or allocates the same position if it is set as static.

ErrorMessage: This is the message that will be displayed in the ValidationSummary (discussed below).

Forecolor: Color of the text displayed. Most often it is red.

SetFocusOnError: This is new in ASP.NET; it will set the focus on the field control.

ValidationGroup: This is new in ASP.NET. The validation group is a name you create for a collection of fields in a form. For example, if you have 2 forms in a one page and you want the submit button of the first form to validate its only fields, you set a common name for all the controls of the first form including the validators. You set this name in the validationgroup property in the validator and you also set it in the field controls where they have a property called validationgroup.

RequiredFieldValidator

The required field validator is used for making the web form field required. The most important property is IntialValue.

IntialValue: This is the value that the validator will validate. The validator will make validation on the value you entered.

RangeValidator

The range validator use is to validate the data that is entered if it is in the valid range you have set in the range validator. The most important properties are the following.

CultureInVariantValues: This is set to check whether or not to do culture invariant conversion against the string values' properties on the validator.

Maximum value: This is the maximum value the control will start validating.

Minimum value: This is the minimum value the control will start validating.

Type: The data type for comparison is the Type. For example, if you choose integer, the validator will check the values entered in the control field if they are within the minimum and maximum ranges.

SetFocusOnError: This is new in ASP.NET; it will set the focus on the field control.

ValidationGroup: This has the same description as above for all validators.

RegularExpressionValidator

The regular expression validator is used to validate an asp.net control to an expression the user specifies. The most popular example is validating the email format of the field. The important properties are below.

ValidationExperssion: This is the place where you choose the standard expression you wish to use in your validation. You can custom your own expression; we will talk about this in another article.

ControlToValidate: You can set the name of the control you want to validate and the name of all controls will display in a list.

Display: the value is Static/Dynamic, it allows the text of the error message displayed to move with the design if it is set as dynamic or allocate the same position if it is set as static.

ErrorMessage: ErrorMessage is the message that will be displayed in the ValidationSummary (discussed below).

Forecolor: Color of the text displayed and is most often red.

SetFocusOnError: This is new in ASP.NET; it will set the focus on the field control.

ValidationGroup: This has the same description as above for all validators.

CompareValidator

The main two essential roles for the compare validator are to compare the entry of 2 fields in a web form and to check datatype of the data entered in the field. The important properties are below.

ControlToCompare: You can choose the ID of the control you want to compare with the ID of the control you set in the ControlToValidate property.

Operator: This is the type of comparison you want to use. For example, the operator type could be equal, greater than, etc. You can also choose the type DataTypeCheck if you wish to make validation on the datatype of the data entered in the field.

Type: You can set in this property the datatype you want to validate the data entered in the control, for example, an integer.

ValueToCompare: You can set in this property the value you want to compare with the entered data in the control.

CustomValidator

The custom validator is used to create your validation function and set to the custom validator. We will talk about it in an article of creating the validation expressions.

ValidationSummary

The validation summary displays the list of all the messages that you set in each validator when the validation fires. The most important properties are the following.

Displaymode: The layout the messages that will display on is the Displaymode.

ShowMessageBox: If set to true, a popup alert window will open displaying all the validation messages of the control.

ShowSummary: If set to true, the validation messages will display on the page.

Text: The message that will appear beside the field when validated is the text.

Conclusion

This was a brief summary about the ASP.NET validations. As we can see, they are very easy and user friendly and can be applied on an ASP.NET WebForm field easily and quickly.


Product Spotlight
Product Spotlight 

©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-04-25 1:07:32 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search