chapter 3 (validation control)

24
Chapter 3 Validation Controls ValidationControl ហោ សូហនឿន 1

Upload: lets-go-to-study

Post on 17-Feb-2017

182 views

Category:

Education


0 download

TRANSCRIPT

Page 1: Chapter 3 (validation control)

Chapter 3

Validation Controls

ValidationControl ហ ោ សូហនឿន 1

Page 2: Chapter 3 (validation control)

Introduction

Used to validate User-Input in an input control.

The page validation is performed when a Button, ImageButton, or Link control is clicked.

Setting CauseValidation to False is to cancel Validation.

ValidationControl ហ ោ សូហនឿន

2

Page 3: Chapter 3 (validation control)

Types of Validation Controls

There are 6 types of Validation Controls RequiredFiledValidator Control

RangeValidator Control

RegularExpressionValidator Control

CompareValidator Control

CustomValidator Control

ValidationSummary Control

ValidationControl ហ ោ សូហនឿន

3

Page 4: Chapter 3 (validation control)

RequiredFieldValidator

To make sure that has entered the required data in the input control(TextBox).

<asp:RequiredFieldValidator runat=server ID=… ControlToValidate=… ErrorMessage=“…."> </asp:RequiredFieldValidator>

ValidationControl ហ ោ សូហនឿន

4

Page 5: Chapter 3 (validation control)

RequiredFieldValidator

See the example:

<body>

<form id="form1" runat="server">

<asp:TextBox ID=fName runat=server> </asp:TextBox>

<asp:RequiredFieldValidator runat=server ID=fNameVal

ControlToValidate=fName ErrorMessage="Please Enter the First

Name"></asp:RequiredFieldValidator>

<asp:Button ID=Submit runat=server Text=Submit />

</form>

</body>

ValidationControl ហ ោ សូហនឿន

5

Page 6: Chapter 3 (validation control)

RangeValidator

To check that the values input is what in the specified range of values.

There are some important properties:

ControlToValidate

MinimumValue

MaximumValue

Type

<asp:RangeValidator runat=server ID=… ControlToValidate=… Type=… MinimumValue=… MaximumValue=… ErrorMessage=“…"> </asp:RangeValidator>

ValidationControl ហ ោ សូហនឿន

6

Page 7: Chapter 3 (validation control)

RangeValidator

See the Example:

<body>

<form id="form1" runat="server">

<asp:TextBox ID=fName runat=server> </asp:TextBox>

<asp:RangeValidator runat=server ControlToValidate=fName MinimumValue="A"

MaximumValue="Z" Type=String ID=fNameVal ErrorMessage="Capital Only"

CultureInvariantValues="True"></asp:RangeValidator> <br />

<asp:Button ID=Submit runat=server Text=Submit />

</form>

</body>

ValidationControl ហ ោ សូហនឿន

7

Page 8: Chapter 3 (validation control)

RegularExpspressionValidator

To make sure the data input into the input box match the specified the format.

It is made up of text with an embedded code started with a backslash (\).

The property ValidateExpression is used the format the data input.

ValidationControl ហ ោ សូហនឿន

8

Page 9: Chapter 3 (validation control)

RegularExpspressionValidator

To set the word boundary use \b ….. \b

[Range of Input]

Example you can set the format for letter input a-z and A-Z as below:

\b[A-Za-z]+\b

<asp:TextBox ID=fName runat=server ></asp:TextBox>

<asp:RegularExpressionValidator runat=server ID=fNameVal ControlToValidate=fName

ValidationExpression="\b[A-Z]+\b" ErrorMessage="Enter the Letter [a-z, A-Z]

only"></asp:RegularExpressionValidator>

ValidationControl ហ ោ សូហនឿន

9

Page 10: Chapter 3 (validation control)

RegularExpspressionValidator

There are some more important forms of the expression format.

\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*

[0-9]{3}-[0-9]{6}

[0-9]{3}-[0-9]{6,7}

ValidationControl ហ ោ សូហនឿន

10

Page 11: Chapter 3 (validation control)

CompareValidator

It is used to compare the value entered by a user into an input box to another.

Some properties used with the CompareValidator are: ControlToCompare

Operator

ValueToCompare

ControlToValidate

Type

ErrorMessage

ValidationControl ហ ោ សូហនឿន

11

Page 12: Chapter 3 (validation control)

CompareValidator

The Operator property has the following value: Equal

NotEqual

GreaterThan

GreaterThanEqual

LessThan

LessThanEqual

DataTypeCheck

ValidationControl ហ ោ សូហនឿន

12

Page 13: Chapter 3 (validation control)

CompareValidator

See the example:

<body>

<form id="form1" runat="server">

User Name: <asp:TextBox ID=txtuser runat=server > </asp:TextBox>

<asp:CompareValidator runat=server ControlToValidate=txtuser

ValueToCompare="sonoeun" ErrorMessage="Invalid User Name">

</asp:CompareValidator> <br />

<asp:Button runat=server ID=bntSubmit Text=Submit />

</form>

</body>

ValidationControl ហ ោ សូហនឿន

13

Page 14: Chapter 3 (validation control)

CustomValidator

To check that the data input matches the a given condition or not.

It is a powerful way to use the Validator controls since you can write your own customization code.

It uses the main properties and methods as below: ClientValidationFunction: To set the name of the custom

client-side script function used for Validation.

ValidateEmptyText: To set a boolean value to make sure if the empty text should be validated or not.

ServerValidate : The method occurring when the validation takes place on the server.

ValidationControl ហ ោ សូហនឿន

14

Page 15: Chapter 3 (validation control)

CustomValidator

See the Example

<body>

<form id="form1" runat="server">

<script language=vbscript runat=server type="text/vbscript" >

Sub serverVal(ByVal source, ByVal objServer)

If (objServer.value = "ServerValidation") Then

objServer.IsValid = True

lbl.Text = "Subscription Successful"

Else

objServer.IsValid = False

lbl.Text = "Subscription Not Successful"

End If

End Sub

</script>

ValidationControl ហ ោ សូហនឿន

15

Page 16: Chapter 3 (validation control)

CustomValidator

See the Example (cont.)

<asp:TextBox ID=ServerObj runat=server ></asp:TextBox>

<asp:CustomValidator ID=ServerObjVal runat=server

OnServerValidate="serverVal" ControlToValidate=ServerObj

ErrorMessage="Subscription NOT Accepted">

</asp:CustomValidator>

<br />

<asp:Button runat=server ID=submitServer Text=Submit />

<br />

<asp:label ID=lbl runat=server ></asp:label>

</form>

</body>

ValidationControl ហ ោ សូហនឿន

16

Page 17: Chapter 3 (validation control)

ValidationSummary

It collects all validation control error messages for centralized display.

It can be displayed as a list, a bullet list, a single paragraph depending on the DisplayMode

Specifying the summary display by setting ShowSummary and ShowMessageBox properties.

ValidationControl ហ ោ សូហនឿន

17

Page 18: Chapter 3 (validation control)

ValidationSummary

Some properties you should know when you use validationSummary are: DisplayedMode

EnableClientScript

ForeColor

HeaderText

ShowMessageBox

ShowSummary

ValidationControl ហ ោ សូហនឿន

18

Page 19: Chapter 3 (validation control)

ValidationSummary

See The Example:

<body>

<form id="form1" runat="server">

First Name:

<asp:TextBox ID=fName runat=server ></asp:TextBox>

<asp:RequiredFieldValidator runat=server ID=RefNameVal

ControlToValidate=fName ErrorMessage="First Name

Needed"></asp:RequiredFieldValidator>

<asp:RegularExpressionValidator runat=server ID=EprfNameVal

ControlToValidate=fName ValidationExpression="\b[A-z]+\b"

ErrorMessage="No Number Permited">

</asp:RegularExpressionValidator>

<br />

Last Name:

<asp:TextBox ID=lName runat=server></asp:TextBox>

ValidationControl ហ ោ សូហនឿន

19

Page 20: Chapter 3 (validation control)

ValidationSummary

See The Example:

<asp:RequiredFieldValidator runat=server ID=RelNameVal

ControlToValidate=lName ErrorMessage="Last Name

Needed"></asp:RequiredFieldValidator>

<asp:RegularExpressionValidator runat=server ID=ExplNameVal

ValidationExpression="\b[A-Z]+\b" ControlToValidate=lName

ErrorMessage="Capital Letter Only">

</asp:RegularExpressionValidator> <br/>

<asp:Button runat=server ID=btnSubmit Text=Submit />

<br />

<asp:ValidationSummary ID=valSummary

ShowMessageBox=true runat=server />

</form>

</body>

ValidationControl ហ ោ សូហនឿន

20

Page 21: Chapter 3 (validation control)

ValidationGroup

It a property of Validator controls.

Setting ValidationGroup, it validates only the validation controls within the specified group when the control is post back to the server.

It is used to assign a validation control to a validation group.

ValidationControl ហ ោ សូហនឿន

21

Page 22: Chapter 3 (validation control)

ValidationGroup

Example

ValidationControl ហ ោ សូហនឿន

22

<body> <form id="form1" runat="server"> <div> <asp:TextBox ID="TextBox1" Runat="server" ValidationGroup="First"> </asp:TextBox> <asp:TextBox ID="TextBox2" Runat="server" ValidationGroup="First"> </asp:TextBox><br /> <asp:RequiredFieldValidator ID="RequiredFieldValidator1" Runat="server" ValidationGroup="First" ErrorMessage="TextBox1 should not be blank" ControlToValidate="TextBox1"> </asp:RequiredFieldValidator> <asp:Button ID="Submit1" Runat="server" ValidationGroup="First" Text="Submit 1" /> <br /> <br />

Page 23: Chapter 3 (validation control)

ValidationGroup

ValidationControl ហ ោ សូហនឿន

23

<asp:TextBox ID="TextBox3" Runat="server" ValidationGroup="Second"> </asp:TextBox> <asp:TextBox ID="TextBox4" Runat="server" ValidationGroup="Second"> </asp:TextBox> <asp:RequiredFieldValidator ID="RequiredFieldValidator2" Runat="server" ErrorMessage=" TextBox3 should not be blank" ControlToValidate="TextBox3" ValidationGroup="Second"> </asp:RequiredFieldValidator> <asp:Button ID="Submit2" Runat="server" ValidationGroup="Second" Text="Submit 2" /> </div> </form> </body>

Page 24: Chapter 3 (validation control)

End

ValidationControl ហ ោ សូហនឿន 24