session 6: validating user input. outline overview of user input validation client-side and...

16
Session 6: Validating User Input

Upload: aubrey-douglas

Post on 21-Jan-2016

221 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Session 6: Validating User Input. Outline Overview of User Input Validation Client-Side and Server-Side Validation ASP.NET Validation Controls Using Validation

Session 6:Validating User Input

Page 2: Session 6: Validating User Input. Outline Overview of User Input Validation Client-Side and Server-Side Validation ASP.NET Validation Controls Using Validation

Outline

Overview of User Input Validation

Client-Side and Server-Side Validation

ASP.NET Validation Controls

Using Validation Controls

Input Validation Controls

RequiredFieldValidator

CompareValidator

RangeValidator

Other Validator Controls

Page Validation

Page 3: Session 6: Validating User Input. Outline Overview of User Input Validation Client-Side and Server-Side Validation ASP.NET Validation Controls Using Validation

What Is Input Validation?

Verifies that a control value is correctly entered by the user

Blocks the processing of a page until all controls are valid

Avoids spoofingor the addition ofmalicious code

Page 4: Session 6: Validating User Input. Outline Overview of User Input Validation Client-Side and Server-Side Validation ASP.NET Validation Controls Using Validation

Client-Side and Server-Side Validation

ASP.NET can create both client-side and server-side validation

Client-side validation Dependent on browser version Instant feedback Reduces postback cycles

Server-side validation Repeats all client-side

validation Can validate against stored

data

Valid?

Valid?

User Enters Data

No

No

Yes

Yes

Error Message

Client

Server

Web ApplicationProcessed

Page 5: Session 6: Validating User Input. Outline Overview of User Input Validation Client-Side and Server-Side Validation ASP.NET Validation Controls Using Validation

ASP.NET Validation Controls

ASP.NET provides validation controls to:

Compare values

Compare to a custom formula

Compare to a range

Compare to a regular expression pattern

Require user input

Summarize the validation controls on a page

Page 6: Session 6: Validating User Input. Outline Overview of User Input Validation Client-Side and Server-Side Validation ASP.NET Validation Controls Using Validation

Adding Validation Controls to a Web Form

1. Add a validation control

2. Select the input control to validate

3. Set validation properties

<asp:Type_of_Validator id="Validator_id"runat="server"ControlToValidate="txtName"ErrorMessage="Message_for_error_summary"Display="static|dynamic|none"Text="Text_to_display_by_input_control">

</asp:Type_of_Validator>

<asp:Type_of_Validator id="Validator_id"runat="server"ControlToValidate="txtName"ErrorMessage="Message_for_error_summary"Display="static|dynamic|none"Text="Text_to_display_by_input_control">

</asp:Type_of_Validator>

<asp:TextBox id="txtName" runat="server" /><asp:TextBox id="txtName" runat="server" />

1111

2222

3333

Page 7: Session 6: Validating User Input. Outline Overview of User Input Validation Client-Side and Server-Side Validation ASP.NET Validation Controls Using Validation

Positioning Validation Controls on a Web Form

Create error messages Select display mode

Static

Dynamic

Page 8: Session 6: Validating User Input. Outline Overview of User Input Validation Client-Side and Server-Side Validation ASP.NET Validation Controls Using Validation

Combining Validation Controls

Can have multiple validation controls on a single input control

Only the RequiredFieldValidator checks empty controls

Page 9: Session 6: Validating User Input. Outline Overview of User Input Validation Client-Side and Server-Side Validation ASP.NET Validation Controls Using Validation

Input Validation Controls

RequiredFieldValidator

CompareValidator

RangeValidator

Page 10: Session 6: Validating User Input. Outline Overview of User Input Validation Client-Side and Server-Side Validation ASP.NET Validation Controls Using Validation

The RequiredFieldValidator Control

The RequiredFieldValidator is used to ensure that a required field is filled in by the user

ControlToValidate (ID of field to validate)

ErrorMessage (if ValidatorSummary is used)

Text (error message to display)

InitialValue (prompt visible in field)

Page 11: Session 6: Validating User Input. Outline Overview of User Input Validation Client-Side and Server-Side Validation ASP.NET Validation Controls Using Validation

The CompareValidator Control

The CompareValidator is used to compare the value entered with (an)other value(s)

ControltoValidate (ID of field to validate)

ValueToCompare (value being compared) or

ControlToCompare (ID of field being compared)

Type (specifies data type to compare to)

Operator (specifies type of comparison)

Page 12: Session 6: Validating User Input. Outline Overview of User Input Validation Client-Side and Server-Side Validation ASP.NET Validation Controls Using Validation

The RangeValidator Control

RangeValidator

ControlToValidate (ID of field to validate)

MinimumValue (lower limit of range)

MaximumValue (upper limit of range)

Type (data type of field)

Page 13: Session 6: Validating User Input. Outline Overview of User Input Validation Client-Side and Server-Side Validation ASP.NET Validation Controls Using Validation

The RegularExpressionValidator Control

Used when input must conform to a pre-defined pattern

Visual Studio .NET includes patterns for:

Telephone numbers

Postal codes

E-mail addresses

<asp:RegularExpressionValidator …ControlToValidate="US_PhoneNumber"…ValidationExpression="((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4} "…>*</asp:RegularExpressionValidator >

<asp:RegularExpressionValidator …ControlToValidate="US_PhoneNumber"…ValidationExpression="((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4} "…>*</asp:RegularExpressionValidator >

Page 14: Session 6: Validating User Input. Outline Overview of User Input Validation Client-Side and Server-Side Validation ASP.NET Validation Controls Using Validation

The CustomValidator Control

Can validate on client-side, server-side, or both

ClientValidationFunction

OnServerValidate

Validate with:

Formula

Data

COM objects

Web Service

Page 15: Session 6: Validating User Input. Outline Overview of User Input Validation Client-Side and Server-Side Validation ASP.NET Validation Controls Using Validation

The Page.IsValid Property

Polls all validation controls

Sub cmdSubmit_Click(s As Object, e As EventArgs)If Page.IsValid Then

Message.Text = "Page is valid!"' Perform database updates or other logic here

End IfEnd Sub

Sub cmdSubmit_Click(s As Object, e As EventArgs)If Page.IsValid Then

Message.Text = "Page is valid!"' Perform database updates or other logic here

End IfEnd Sub

Page 16: Session 6: Validating User Input. Outline Overview of User Input Validation Client-Side and Server-Side Validation ASP.NET Validation Controls Using Validation

Using the ValidationSummary Control

Collects error messages from all validation controls on the page

Can display text and error messages

Use Text="*" to indicate the location of the error

<asp:ValidationSummary id="valSummary"runat="server"HeaderText="These errors were found:"ShowSummary="True" DisplayMode="List"/>

<asp:ValidationSummary id="valSummary"runat="server"HeaderText="These errors were found:"ShowSummary="True" DisplayMode="List"/>