Chapter 3
Validation
Controls
ValidationControl ហ ោសូហនឿន 1
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
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
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
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
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
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
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
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
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
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
CompareValidator
 The Operator property has the following value:
 Equal
 NotEqual
 GreaterThan
 GreaterThanEqual
 LessThan
 LessThanEqual
 DataTypeCheck
ValidationControlហ ោសូហនឿន
12
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
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
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
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
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
ValidationSummary
 Some properties you should know when you use
validationSummary are:
 DisplayedMode
 EnableClientScript
 ForeColor
 HeaderText
 ShowMessageBox
 ShowSummary
ValidationControlហ ោសូហនឿន
18
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
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
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
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 />
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>
End
ValidationControl ហ ោសូហនឿន 24

Chapter 3 (validation control)

  • 1.
  • 2.
    Introduction  Used tovalidate 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
  • 3.
    Types of ValidationControls  There are 6 types of Validation Controls  RequiredFiledValidator Control  RangeValidator Control  RegularExpressionValidator Control  CompareValidator Control  CustomValidator Control  ValidationSummary Control ValidationControlហ ោសូហនឿន 3
  • 4.
    RequiredFieldValidator  To makesure that has entered the required data in the input control(TextBox). <asp:RequiredFieldValidator runat=server ID=… ControlToValidate=… ErrorMessage=“…."> </asp:RequiredFieldValidator> ValidationControlហ ោសូហនឿន 4
  • 5.
    RequiredFieldValidator  See theexample: <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
  • 6.
    RangeValidator  To checkthat 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
  • 7.
    RangeValidator  See theExample: <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
  • 8.
    RegularExpspressionValidator  To makesure 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
  • 9.
    RegularExpspressionValidator  To setthe 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
  • 10.
    RegularExpspressionValidator  There aresome 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
  • 11.
    CompareValidator  It isused 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
  • 12.
    CompareValidator  The Operatorproperty has the following value:  Equal  NotEqual  GreaterThan  GreaterThanEqual  LessThan  LessThanEqual  DataTypeCheck ValidationControlហ ោសូហនឿន 12
  • 13.
    CompareValidator  See theexample: <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
  • 14.
    CustomValidator  To checkthat 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
  • 15.
    CustomValidator  See theExample <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
  • 16.
    CustomValidator  See theExample (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
  • 17.
    ValidationSummary  It collectsall 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
  • 18.
    ValidationSummary  Some propertiesyou should know when you use validationSummary are:  DisplayedMode  EnableClientScript  ForeColor  HeaderText  ShowMessageBox  ShowSummary ValidationControlហ ោសូហនឿន 18
  • 19.
    ValidationSummary  See TheExample: <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
  • 20.
    ValidationSummary  See TheExample: <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
  • 21.
    ValidationGroup  It aproperty 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
  • 22.
    ValidationGroup  Example ValidationControlហ ោសូហនឿន 22 <body> <formid="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 />
  • 23.
    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>
  • 24.