Sunteți pe pagina 1din 19

Module 6

Validating User Input

Module 6: Validating User Input


Overview of User Input Validation ASP.NET Validation Controls

Validating Web Forms

Lesson 1: Overview of User Input Validation


What Is Input Validation? Client-Side and Server-Side Validation

What Is Input Validation?

Input validation verifies that the user has correctly filled in an input
control, before the request is processed on the server

Input validation:

Compares user input against a predetermined input format


Blocks the processing of a page until all user input is validated Prevents spoofing or the addition of malicious code

Client-Side and Server-Side Validation


Client-side validation:
Is dependent on the
browser version Provides instant feedback Reduces post-back cycles Client

User Enters Data

Error Message

Valid?
No Yes

Server

Server-side validation:
Repeats all client-side validation Validates against stored data or other server-side resources

Valid?
Yes

No

Web Application Processed

Lesson 2: ASP.NET Validation Controls


Overview of ASP.NET Validation Controls Basic ASP.NET Validation Controls

RegularExpressionValidator Control
CustomValidator Control Combining Validation Controls

Adding Validation Controls to a Web Form


Positioning and Configuring Validation Controls on a Web

Form

Overview of ASP.NET Validation Controls


ASP.NET provides a set of validation controls that helps you check for input errors

Validation Control
RequiredFieldValidator CompareValidator

Function
Requires user input Compares input values

RangeValidator
RegularExpressionValidator

Compares input values to a range


Compares input values to a regular expression pattern Compares input values to custom logic

CustomValidator

ValidationSummary

Summarizes the validation controls on a page

Basic ASP.NET Validation Controls


RequiredFieldValidator InitialValue

CompareValidator

ValueToCompare or ControlToCompare Type Operator


RangeValidator MinimumValue MaximumValue Type

RegularExpressionValidator Control
The RegularExpressionValidator control verifies if the user input matches a predefined pattern Visual Studio 2008 SP1 Regular Expression Editor includes patterns for: Telephone numbers Postal codes and zip codes E-mail addresses URLs Social security numbers
<asp:RegularExpressionValidator id="EmailRegexValidator" runat="server" ControlToValidate="EmailTextBox" ErrorMessage="Use the format username@organization.xxx" ValidationExpression="\w+@\w+\.\w+" Text="*" /> The following markup shows how you can use a RegularExpressionValidator control to check if a user has entered a valid e-mail address <asp:TextBox id="EmailTextBox" runat="server" />

CustomValidator Control

The CustomValidator control provides custom validation logic

You can validate on the client side and server side by using the: ClientValidationFunction method OnServerValidate method

Combining Validation Controls


The following code shows how multiple validation controls can be associated with a single input control
TextBox <asp:TextBox id="PhoneTextBox" runat="server" /> <asp:RequiredFieldValidator id="PhoneRequiredFieldValidator" runat="server" ErrorMessage="The telephone number is required." ControlToValidate="PhoneTextBox" Text="*" /> <asp:RegularExpressionValidator id="PhoneRegularExpressionValidator" runat="server" ErrorMessage="The telephone number is not formatted as a correct US phone number." ControlToValidate="PhoneTextBox" ValidationExpression= "((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4} Text="*" /> <asp:CustomValidator id="PhoneCustomValidator" OnServerValidate="PhoneServerValidationHandler" runat="server" ErrorMessage="This telephone number is not recognized" ControlToValidate="PhoneTextBox" Text="*" />

RequiredFiel dValidator

RegularExp ressionVali dator

CustomVali dator

Adding the ValidationSummary Control


The ValidationSummary control displays error messages when the Page.IsValid property returns false The following code shows how to add the ValidationSummary control <asp:ValidationSummary

id="MyValidationSummary"
runat="server" HeaderText="These errors were found:" ShowSummary="True"

DisplayMode="List" />

Application, Session and Cookies

An application state stores global information used across multiple session and requests. The Application object generally holds information that will be used by multiple pages of the application

13

Application, Session and Cookies

Application objects are used to reference instances of HttpApplicationState class


The Lock() method prevents other users from altering the variables stored in the Application object.

The Unlock() method is used to unlock the locked variables stored in the Application object.
Properties Method

AllKeys Contents Count Item StaticObjects

Add Clear Remove RemoveAll RemoveAt


14

Application, Session and Cookies


Cookies storing such information that is remembered across multiple session are referred to as persistent cookies.

Persistent cookies are also known as permanent cookies or stored cookies.


Persistent cookies have an expiry date and stored on to users hard disk until the expiry date or until they are manually deleted.

15

Application, Session and Cookies

Session cookie is also referred to as a transient cookie.


Session cookies are stored temporarily in the memory. One the browser is closed, the session cookie cannot be retained.

16

Application, Session and Cookies


Session variables are used to store information about a single user session Session variables are cleared as soon as the users session at the site comes to an end

Sessions are identified by a session identifier, which is unique.


The sessions identifier can be read using SessionID property.

Events

17

Global.asax
Global.asax file is referred to as an ASP.Net application file , which contains the code for responding to application or module-level events in one central location.

The Global.asax file is an optional file and created only if the application or session events need to handled
The Global.asax file is located in root application directory with .asax extension

18

Global.asax

When a new ASP.Net application is created with Visual Studio 2005 IDE , a Global.asax file is automatically added to project. Every ASP.Net Web applocation can have one, and only Global.asax file

19

S-ar putea să vă placă și