1 Validation Chapter 9
Jan 11, 2016
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.
4
Validation Controls
RequiredFieldValidator RangeValidator CompareValidator RegularExpressionValidator CustomValidator
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.
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.
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.
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.
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.
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.
11
Design the Page
Expand the Validation section of the Toolbox.
Add a RangeValidator beside the TextBox.
12
RangeValidator
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.
14
Design the Page
Add another TextBox with no validator.
Add an OK button. btnOK
Add a label beside the button. lblMessage
15
The Page in Design View
16
Add Click Event Handler
Double click on the button to add a Click Event Handler.
17
Page in Source Mode
18
Set Breakpoint
Set a breakpoint on the Page_Load method. So that we can tell when a postback occurs.
19
Program Running
Enter an invalid number and press tab
20
After User Presses Tab Key
Note that the page did not post back.
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.
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.
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.
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.
25
Invalid Input
26
Valid Input
27
No Input
If nothing is entered, the range validation passes.
If we want to require input we need another validator.
RequiredFieldValidator
28
Add RequiredFieldValidator
29
RequiredFieldValidator Properties
Try it!
30
RequiredFieldValidator
Click on OK with no inputs.
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
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"
33
RangeValidator
34
RequiredFieldValidator
35
Validation Summary
36
Validation Summary
Try it!
37
Validation Summary
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.
39
Display Property Set To None
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
41
Flag Invalid Input
42
Other Display Options
43
Other Properties of ValidationSummary
Border properties DisplayMode HeaderText
ShowMessageBox EnableClientScript must be true.
ShowSummary Set to false to disable summary
44
Validation Summary Properties
45
RangeValidator1
46
Validation Summary
47
Message Box Summary
48
EnableClientScript
49
Message Box Summary
End of Presentation