Top Banner
1 Validation Chapter 9
49

1 Validation Chapter 9. 2 Objectives You will be able to use validation controls on your web pages to validate user inputs automatically.

Jan 11, 2016

Download

Documents

Lilian Randall
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 1 Validation Chapter 9. 2 Objectives You will be able to use validation controls on your web pages to validate user inputs automatically.

1

Validation

Chapter 9

Page 2: 1 Validation Chapter 9. 2 Objectives You will be able to use validation controls on your web pages to validate user inputs automatically.

2

Objectives

You will be able to use validation controls on your web pages to validate user inputs automatically.

Page 3: 1 Validation Chapter 9. 2 Objectives You will be able to use validation controls on your web pages to validate user inputs automatically.

3

Validation Controls

Validation controls permit declarative validation of user inputs. Specify conditions declaratively rather

than writing code to check validity.

Page 4: 1 Validation Chapter 9. 2 Objectives You will be able to use validation controls on your web pages to validate user inputs automatically.

4

Validation Controls

RequiredFieldValidator RangeValidator CompareValidator RegularExpressionValidator CustomValidator

Page 5: 1 Validation Chapter 9. 2 Objectives You will be able to use validation controls on your web pages to validate user inputs automatically.

5

Validation Controls

Each validation control is bound to a single input control.

One input control can have multiple validation controls.

Normally an invalid input will prevent the postback from being done. Require user to provide valid inputs

before passing them to the server.

Page 6: 1 Validation Chapter 9. 2 Objectives You will be able to use validation controls on your web pages to validate user inputs automatically.

6

CausesValidation

Every button has a CausesValidation property.

Checked when button is clicked, before page is posted back.

If true, validation is performend on all input controls that have validation controls. In the browser. Again on the server.

If false, page is posted back without validation.

Page 7: 1 Validation Chapter 9. 2 Objectives You will be able to use validation controls on your web pages to validate user inputs automatically.

7

Why not do validation?

Consider a Cancel button.

Sometimes we want to be able to do a postback even if some inputs are invalid.

Page 8: 1 Validation Chapter 9. 2 Objectives You will be able to use validation controls on your web pages to validate user inputs automatically.

8

Client-Side Validation

ASP.NET transparently provides JavaScript to do validation in the browser.

Supported by most modern browsers Chrome Firefox Safari Internet Explorer 5 and later

If client-side validation fails, page does not post back. Error information is shown on the page.

Page 9: 1 Validation Chapter 9. 2 Objectives You will be able to use validation controls on your web pages to validate user inputs automatically.

9

Server-Side Validation

Even if page passes all validation checks on the browser, inputs will be validated on the server.

Hackers can easily bypass client-side validation. Some users may disable JavaScript.

Client-side validation is a matter of convenience and performance only. Not security.

Server-side code has the ultimate responsibility for ensuring that inputs are valid.

Page 10: 1 Validation Chapter 9. 2 Objectives You will be able to use validation controls on your web pages to validate user inputs automatically.

10

Simple Example

Create a new empty ASPX C# website Validation_Demo

Add Default.aspx

We will design a page with a TextBox having a range validator.

Second TextBox with no validator.

Page 11: 1 Validation Chapter 9. 2 Objectives You will be able to use validation controls on your web pages to validate user inputs automatically.

11

Design the Page

Expand the Validation section of the Toolbox.

Add a RangeValidator beside the TextBox.

Page 12: 1 Validation Chapter 9. 2 Objectives You will be able to use validation controls on your web pages to validate user inputs automatically.

12

RangeValidator

Page 13: 1 Validation Chapter 9. 2 Objectives You will be able to use validation controls on your web pages to validate user inputs automatically.

13

Display Property Values

Static Reserve space on page for the error message

even if the control is valid.

Dynamic Do not reserve space on page for the error

message if the control is valid. Useful when there are mulitple validators for a

single control.

None Do not display the error message.

Page 14: 1 Validation Chapter 9. 2 Objectives You will be able to use validation controls on your web pages to validate user inputs automatically.

14

Design the Page

Add another TextBox with no validator.

Add an OK button. btnOK

Add a label beside the button. lblMessage

Page 15: 1 Validation Chapter 9. 2 Objectives You will be able to use validation controls on your web pages to validate user inputs automatically.

15

The Page in Design View

Page 16: 1 Validation Chapter 9. 2 Objectives You will be able to use validation controls on your web pages to validate user inputs automatically.

16

Add Click Event Handler

Double click on the button to add a Click Event Handler.

Page 17: 1 Validation Chapter 9. 2 Objectives You will be able to use validation controls on your web pages to validate user inputs automatically.

17

Page in Source Mode

Page 18: 1 Validation Chapter 9. 2 Objectives You will be able to use validation controls on your web pages to validate user inputs automatically.

18

Set Breakpoint

Set a breakpoint on the Page_Load method. So that we can tell when a postback occurs.

Page 19: 1 Validation Chapter 9. 2 Objectives You will be able to use validation controls on your web pages to validate user inputs automatically.

19

Program Running

Enter an invalid number and press tab

Page 20: 1 Validation Chapter 9. 2 Objectives You will be able to use validation controls on your web pages to validate user inputs automatically.

20

After User Presses Tab Key

Note that the page did not post back.

Page 21: 1 Validation Chapter 9. 2 Objectives You will be able to use validation controls on your web pages to validate user inputs automatically.

21

Server-Side Validation

To see how the page will work on a browser that does not support JavaScript set RangeValidator1.EnableClientScript property to false.

Try again.

Page 22: 1 Validation Chapter 9. 2 Objectives You will be able to use validation controls on your web pages to validate user inputs automatically.

22

After User Clicks OK

Nothing happens when we tab out of the first box.

Error message is shown after postback when we click OK.

Page 23: 1 Validation Chapter 9. 2 Objectives You will be able to use validation controls on your web pages to validate user inputs automatically.

23

Server-Side Processing

Note that the error message appeared on the page, BUT the click event handler executed even

thought the input was invalid.

We can prevent the normal click actions if the page is invalid but we have to provide an explicit

check.

Page 24: 1 Validation Chapter 9. 2 Objectives You will be able to use validation controls on your web pages to validate user inputs automatically.

24

Server-Side Validity Check

protected void btnOK_Click(object sender, EventArgs e)

{

if (Page.IsValid)

{

lblMessage.Text = "btnOK_Click event handler executed";

}

else

{

lblMessage.Text = "Input is invalid";

}

}

Try again with invalid input.

Page 25: 1 Validation Chapter 9. 2 Objectives You will be able to use validation controls on your web pages to validate user inputs automatically.

25

Invalid Input

Page 26: 1 Validation Chapter 9. 2 Objectives You will be able to use validation controls on your web pages to validate user inputs automatically.

26

Valid Input

Page 27: 1 Validation Chapter 9. 2 Objectives You will be able to use validation controls on your web pages to validate user inputs automatically.

27

No Input

If nothing is entered, the range validation passes.

If we want to require input we need another validator.

RequiredFieldValidator

Page 28: 1 Validation Chapter 9. 2 Objectives You will be able to use validation controls on your web pages to validate user inputs automatically.

28

Add RequiredFieldValidator

Page 29: 1 Validation Chapter 9. 2 Objectives You will be able to use validation controls on your web pages to validate user inputs automatically.

29

RequiredFieldValidator Properties

Try it!

Page 30: 1 Validation Chapter 9. 2 Objectives You will be able to use validation controls on your web pages to validate user inputs automatically.

30

RequiredFieldValidator

Click on OK with no inputs.

Page 31: 1 Validation Chapter 9. 2 Objectives You will be able to use validation controls on your web pages to validate user inputs automatically.

31

Validation Summary

The validation summary control shows all error messages from the page together in one place.

Add a ValidationSummary below the OK button.

Modify the error messages to identify which input it applies to

Page 32: 1 Validation Chapter 9. 2 Objectives You will be able to use validation controls on your web pages to validate user inputs automatically.

32

Display Options

If a validation control’s Text property is set, it will be shown at the position of the validator, but the ErrorMessage will be shown in the Error Summary.

Example: Text: "This input is required" ErrorMessage: "First input is required"

Page 33: 1 Validation Chapter 9. 2 Objectives You will be able to use validation controls on your web pages to validate user inputs automatically.

33

RangeValidator

Page 34: 1 Validation Chapter 9. 2 Objectives You will be able to use validation controls on your web pages to validate user inputs automatically.

34

RequiredFieldValidator

Page 35: 1 Validation Chapter 9. 2 Objectives You will be able to use validation controls on your web pages to validate user inputs automatically.

35

Validation Summary

Page 36: 1 Validation Chapter 9. 2 Objectives You will be able to use validation controls on your web pages to validate user inputs automatically.

36

Validation Summary

Try it!

Page 37: 1 Validation Chapter 9. 2 Objectives You will be able to use validation controls on your web pages to validate user inputs automatically.

37

Validation Summary

Page 38: 1 Validation Chapter 9. 2 Objectives You will be able to use validation controls on your web pages to validate user inputs automatically.

38

Summary Only

We might want to supress the error message at the position of the input. Avoid clutter with lots of validated

inputs.

Set its Display property to None.

Page 39: 1 Validation Chapter 9. 2 Objectives You will be able to use validation controls on your web pages to validate user inputs automatically.

39

Display Property Set To None

Page 40: 1 Validation Chapter 9. 2 Objectives You will be able to use validation controls on your web pages to validate user inputs automatically.

40

Other Display Options

If a page has a lot of inputs, we might want to just flag the invalid inputs and put the detailed information in the summary.

Example: Flag the input field with an invalid

value. Set Text to “*” Set Display back to Static

Page 41: 1 Validation Chapter 9. 2 Objectives You will be able to use validation controls on your web pages to validate user inputs automatically.

41

Flag Invalid Input

Page 42: 1 Validation Chapter 9. 2 Objectives You will be able to use validation controls on your web pages to validate user inputs automatically.

42

Other Display Options

Page 43: 1 Validation Chapter 9. 2 Objectives You will be able to use validation controls on your web pages to validate user inputs automatically.

43

Other Properties of ValidationSummary

Border properties DisplayMode HeaderText

ShowMessageBox EnableClientScript must be true.

ShowSummary Set to false to disable summary

Page 44: 1 Validation Chapter 9. 2 Objectives You will be able to use validation controls on your web pages to validate user inputs automatically.

44

Validation Summary Properties

Page 45: 1 Validation Chapter 9. 2 Objectives You will be able to use validation controls on your web pages to validate user inputs automatically.

45

RangeValidator1

Page 46: 1 Validation Chapter 9. 2 Objectives You will be able to use validation controls on your web pages to validate user inputs automatically.

46

Validation Summary

Page 47: 1 Validation Chapter 9. 2 Objectives You will be able to use validation controls on your web pages to validate user inputs automatically.

47

Message Box Summary

Page 48: 1 Validation Chapter 9. 2 Objectives You will be able to use validation controls on your web pages to validate user inputs automatically.

48

EnableClientScript

Page 49: 1 Validation Chapter 9. 2 Objectives You will be able to use validation controls on your web pages to validate user inputs automatically.

49

Message Box Summary

End of Presentation