Validating User Input in ASP.NET

validation much easier and reduce the amount of code that the developer must write to perform page validation. The ASP.NET team reviewed numerous Web sites to determine the most common types of validation that were taking place on the Web. Most developers were reinventing the wheel to perform validation, so the ASP.NET team decided that Web developers needed a set of validation controls to add to their toolbox.

From the start, these controls were designed to detect the version of the browser when used in client-side validation and then render the correct version of HTML for that client browser.

These research efforts lead to the development of the six controls covered in this chapter. The examples in this chapter will take a look at each control and explain the most commonly used properties for each control. However, keep in mind that all of the controls share basic properties, such as font, fore color, back color, and so on .

Everything in the .NET Framework is a class, and the validation controls are no exception. All validation controls in the .NET Framework are derived from the BaseValidator class. This class serves as the base abstract class for the validation controls and provides the core implementation for all validation controls that derive from this class. Validation controls always perform validation checking on the server.

Validation controls also have complete client-side implementation that allows browsers that support DHTML to perform validation on the client. Client-side validation enhances the validation scheme by checking user input as the user enters data. This allows errors to be detected on the client before the form is submitted, preventing the round trip necessary for server-side validation. In addition, more than one validator may be used on a page to validate different aspects.

Before you look at the validation controls, step back in time before .NET and take a look at a simplified example of performing validation with ASP. Launch your browser and run the BeforeDotNet.asp code.

Using the RequiredFieldValidator Control

Use the RequiredFieldValidator control when a value is required for an input element on the Web page. This control checks whether the value of the associated input control is different from its initial value. You can easily convert the previous sample ASP code into .NET with the RequiredFieldValidator control. Take a look at the following code fragment:

FIRST id="rfvFirst"
controltovalidate="First"
display="dynamic"
errormessage="Please enter your first name."
runat="Server">

Launch your browser and navigate to Required.aspx code .

If all went well, you should see the validation message "Please enter your first name." on the page. Now enter your name and click the Validate button again. This time you should see the message, "Hello, your name" displayed on the page.

ControlToValidate property

Set this property's value to the name of the control to validate. If you refer to the code listing, this property was set to First to indicate that a value is required for this control.

Display property

You can set this property to one of three values to determine how the error message is displayed on the page if validation fails:

Dynamic: The validator is displayed inline on the Web page if validation fails. The validator only takes up space on the page when the validator is visible. This allows multiple validators to occupy the same physical location on the Web page when those validators become visible. To
avoid the Web page layout changing the validator becomes vi sible, the HTML element containing the validator must be large enough to accommodate the size of the validator.

Static: The validator is displayed inline on the Web page if validation fails. Also, if the validator is hidden and becomes visible, the page layout does not change.

None: The validation contents are not displayed inline on the Web page, the error message is displayed in the ValidationSummary control.

Using the CompareValidator Control

Use the CompareValidator control to make sure that a value matches a specified value.
This control compares the value of an input control to another input control or a constant value using a variety of operators and types. You can also use this control to make sure that your input value is of a specific type: integer, string, and so on.

Using the RangeValidator Control

Use the RangeValidator control to determine whether a value falls within the specified range. It checks whether the value of the associated input control is within some minimum and maximum, which can be a constant value or the value of another control.

Type property

You can set the Type property to one of the following values, which determines the type of values in the range:

*Currency: The data type is Currency.
*Date: The data type is DateTime.
*Double: The data type is Double.
*Integer: The data type is Integer.
*String: The data type is String.

MinimumValue and MaximumValue properties

The RangeValidator control is useful when you need to make sure that a value falls within a specified range of values. Take a look at the code fragment that follows for the RangeValidator control.

The RequiredFieldValidator control (which requires a field on the Web page to have value);
The CompareValidator control (which allows us to compare values in one control to another control on the page, or a specified minimum and maximum value); the RangeValidator control (which allows us to compare the value of a control on the Web page to a range of values); and the RegularExpression Validator control (which we can use to validate a value against regular expressions).

Also, we have the CustomValidator control, so that if none of the other validation controls fit our needs, then we can write our own validation routine. Finally, we discussed the ValidationSummary control, which allows us to accumulate all of the error messages from the other controls and display them as a list on the Web page.

As you can see, the .NET Framework provides Web developers with a great set of controls for doing page validation, instead of spending time writing validation code.

RELATED POST

CREATING ASSEMBLIES WITH ASP.NET

BUILDING HANDLERS IN ASP.NET

INTERFACES AND CLASSES CREATION IN ASP.NET

CACHING IN ASP.NET

CACHING IN ASP.NET PART TWO

WIRE LESS APPLICATION WITH ASP.NET

SECURITY IN ASP.NET PART ONE

SECURITY IN ASP.NET PART TWO

LOCALIZING ASP.NET APPLICATIONS

DEPLOYING ASP.NET APPLICATIONS

ASP.NET CONFIGURATION SYSTEM

WEB SERVICES IN ASP.NET

WEB SERVICES PART TWO

WEB SERVICE INFRASTRUCTURE

WEB SERVICE INFRASTRUCTURE PART TWO

EXCHANGING MESSAGES IN ASP.NET

MICROSOFT HAIL STORM

SOAP AND DOT NET PART ONE

SOAP AND DOT NET PART TWO

No comments:

Post a Comment