0% found this document useful (0 votes)
32 views11 pages

Validation - Controls

The document discusses different validation controls in ASP.NET - CompareValidator, CustomValidator, and RangeValidator. It provides properties and examples of each control. The CompareValidator allows comparing a value to another control or constant. CustomValidator uses custom validation logic specified in code. RangeValidator checks if a value is within a specified minimum and maximum.

Uploaded by

jignesh kante
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views11 pages

Validation - Controls

The document discusses different validation controls in ASP.NET - CompareValidator, CustomValidator, and RangeValidator. It provides properties and examples of each control. The CompareValidator allows comparing a value to another control or constant. CustomValidator uses custom validation logic specified in code. RangeValidator checks if a value is within a specified minimum and maximum.

Uploaded by

jignesh kante
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

WEB PROG. USING ASP.

NET IT Department
5th Semester VALIDATION CONTROLS Krunal Prajapati

COMPARE VALIDATOR

 The CompareValidator control allows you to compare the value entered by the user into
an input control, such as a TextBox control, with the value entered into another input
control, or with a constant value. You can also use the CompareValidator control to
determine whether the value entered into an input control can be converted to the data
type specified by the Type property.

PROPERTIES

PROPERTY DESCRIPTION
BackColor The background color of the CompareValidator control
ControlToCompare The name of the control to compare with
ControlToValidate The id of the control to validate
Display The display behavior for the validation control. Legal values are:

 None (the control is not displayed. Used to show the error message
only in the ValidationSummary control)
 Static (the control displays an error message if validation fails. Space
is reserved on the page for the message even if the input passes
validation.
 Dynamic (the control displays an error message if validation fails.
Space is not reserved on the page for the message if the input
passes validation

EnableClientScript A Boolean value that specifies whether client-side validation is enabled or


not
Enabled A Boolean value that specifies whether the validation control is enabled or
not
ErrorMessage The text to display in the ValidationSummary control when validation
fails.Note: This text will also be displayed in the validation control if the
Text property is not set
ForeColor The foreground color of the control
id A unique id for the control
Operator The type of comparison to perform. The operators are:

 Equal
 GreaterThan
 GreaterThanEqual
 LessThan
 LessThanEqual
 NotEqual
 DataTypeCheck

runat Specifies that the control is a server control. Must be set to "server"
Text The message to display when validation fails
WEB PROG. USING ASP.NET
6th Semester VALIDATION CONTROLS Krunal Prajapati

Type Specifies the data type of the values to compare. The types are:

 Currency
 Date
 Double
 Integer
 String

ValueToCompare A specified value to compare with

EXAMPLE

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


<div>
<h4>Compare two values</h4>

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


<asp:TextBox id="txt2" runat="server" />
<asp:Button ID="Button1" Text="Validate" runat="server" />
<br />
<asp:CompareValidator
id="compval"
Display="dynamic"
ControlToValidate="txt1"
ControlToCompare="txt2"
ForeColor="red"
BackColor="yellow"
Type="String"
EnableClientScript="False"
Text="Validation Failed!"
runat="server" />
</div>
</form>

OUTPUT

Page 2 of 11
WEB PROG. USING ASP.NET
6th Semester VALIDATION CONTROLS Krunal Prajapati

CUSTOM VALIDATOR

 The CustomValidator control allows you to create a validation control with customized
validation logic. For example, you can create a validation control that checks whether the
value entered into a text box is an even number.

PROPERTIES

BackColor The background color of the CustomValidator control


ClientValidationFunction Specifies the name of the client-side validation script function to be
executed.Note: The script must be in a language that the browser supports,
such as VBScript or JScript

With VBScript, the function must be in the form:

Sub FunctionName (source, arguments)

With JScript, the function must be in the form:

Function FunctionName (source, arguments)


ControlToValidate The id of the control to validate
Display The display behavior for the validation control. Legal values are:

 None (the control is not displayed. Used to show the error message
only in the ValidationSummary control)
 Static (the control displays an error message if validation fails. Space
is reserved on the page for the message even if the input passes
validation.
 Dynamic (the control displays an error message if validation fails.
Space is not reserved on the page for the message if the input passes
validation

EnableClientScript A Boolean value that specifies whether client-side validation is enabled or


not
Enabled A Boolean value that specifies whether the validation control is enabled or
not
ErrorMessage The text to display in the ValidationSummary control when validation
fails.Note: This text will also be displayed in the validation control if the Text
property is not set
ForeColor The foreground color of the control
id A unique id for the control
OnServerValidate Specifies the name of the server-side validation script function to be
executed
runat Specifies that the control is a server control. Must be set to "server"
Text The message to display when validation fails

Page 3 of 11
WEB PROG. USING ASP.NET
6th Semester VALIDATION CONTROLS Krunal Prajapati

EXAMPLE

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


<div>
<asp:Label ID="Label1" runat="server" Text="Enter a username: " />
<asp:TextBox id="txt1" runat="server" />
<asp:Button ID="Button1" Text="Submit" runat="server"/>
<br />
<asp:Label id="msg" runat="server"/>
<br />
<asp:CustomValidator
ID="CustomValidator1"
ControlToValidate="txt1"
OnServerValidate="user1"
Text="A username must be between 8 and 16 characters!"
runat="server"/>
</div>
</form>

VB CODE

Sub user1(ByVal source As Object, ByVal args As ServerValidateEventArgs)


If Len(args.Value) < 8 Or Len(args.Value) > 16 Then
args.IsValid = False
Else
args.IsValid = True
End If
End Sub

OUTPUT

Page 4 of 11
WEB PROG. USING ASP.NET
6th Semester VALIDATION CONTROLS Krunal Prajapati

RANGE VALIDATOR

 The RangeValidator control allows you to check whether a user's entry is between a
specified upper and lower boundary. You can check ranges within pairs of numbers,
alphabetic characters, and dates.

Note: If the input control is empty, no validation functions are called and validation
succeeds. Use a RequiredFieldValidator control to prevent the user from skipping an input
control.

PROPERTIES

BackColor The background color of the RangeValidator control


ControlToValidate The id of the control to validate
Display The display behavior for the validation control. Legal values are:

 None (the control is not displayed. Used to show the error message only in
the ValidationSummary control)
 Static (the control displays an error message if validation fails. Space is
reserved on the page for the message even if the input passes validation.
 Dynamic (the control displays an error message if validation fails. Space is
not reserved on the page for the message if the input passes validation

EnableClientScript A Boolean value that specifies whether client-side validation is enabled or not
Enabled A Boolean value that specifies whether the validation control is enabled or not
ErrorMessage The text to display in the ValidationSummary control when validation
fails. Note:This text will also be displayed in the validation control if the Text
property is not set
ForeColor The foreground color of the control
id A unique id for the control
IsValid A Boolean value that indicates whether the control specified by ControlToValidate
is determined to be valid
MaximumValue Specifies the maximum value of the input control
MinimumValue Specifies the minimum value of the input control
runat Specifies that the control is a server control. Must be set to "server"
Type Specifies the data type of the value to check. The types are:

 Currency
 Date
 Double
 Integer
 String

Text The message to display when validation fails

Page 5 of 11
WEB PROG. USING ASP.NET
6th Semester VALIDATION CONTROLS Krunal Prajapati

EXAMPLE

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


<div>
Enter a date between 2005-01-01 and 2005-12-31:
<br />
<asp:TextBox id="tbox1" runat="server" />
<br /><br />
<asp:Button ID="Button1" Text="Submit" runat="server" />
<br /><br />
<asp:RangeValidator
ID="RangeValidator1"
ControlToValidate="tbox1"
MinimumValue="2005-01-01"
MaximumValue="2005-12-31"
Type="Date"
EnableClientScript="False"
Text="The date must be between 2005-01-01 and 2005-12-31!"
runat="server" />
</div>
</form>

OUTPUT

Page 6 of 11
WEB PROG. USING ASP.NET
6th Semester VALIDATION CONTROLS Krunal Prajapati

REGULAREXPRESSION VALIDATOR

 The RegularExpressionValidator control is used to determine whether the value of an


input control matches a pattern defined by a regular expression. This type of validation
allows you to check for predictable sequences of characters, such as those in social security
numbers, e-mail addresses, telephone numbers, postal codes, and so on.

Note: If the input control is empty, no validation functions are called and validation
succeeds. Use a RequiredFieldValidator control to prevent the user from skipping an input
control.

PROPERTIES

BackColor The background color of the RegularExpressionValidator control


ControlToValidate The id of the control to validate
Display The display behavior for the validation control. Legal values are:

 None (the control is not displayed. Used to show the error message only
in the ValidationSummary control)
 Static (the control displays an error message if validation fails. Space is
reserved on the page for the message even if the input passes validation.
 Dynamic (the control displays an error message if validation fails. Space
is not reserved on the page for the message if the input passes validation

EnableClientScript A Boolean value that specifies whether client-side validation is enabled or not
Enabled A Boolean value that specifies whether the validation control is enabled or not
ErrorMessage The text to display in the ValidationSummary control when validation
fails.Note: This text will also be displayed in the validation control if the Text
property is not set
ForeColor The foreground color of the control
id A unique id for the control
IsValid A Boolean value that indicates whether the control specified by
ControlToValidate is determined to be valid
runat Specifies that the control is a server control. Must be set to "server"
BackColor The background color of the RegularExpressionValidator control
Text The message to display when validation fails
ValidationExpression Specifies the expression used to validate input control. The expression validation
syntax is different on the client than on the server. JScript is used on the client.
On the server, the language you have specified is used

Page 7 of 11
WEB PROG. USING ASP.NET
6th Semester VALIDATION CONTROLS Krunal Prajapati

EXAMPLE

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


<div>
Enter E-mail ID:
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Submit" />
<br /><br />
<asp:RegularExpressionValidator
ID="RegularExpressionValidator1"
ControlToValidate="TextBox1"
ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"
ErrorMessage="Email ID you entered is not valid."
runat="server" />
</div>
</form>

OUTPUT

REQUIREDFIELD VALIDATOR

 Use the RequiredFieldValidator control to make an input control a mandatory field. The
input control fails validation if the value it contains does not change from its initial value
when validation is performed. This prevents the user from leaving the associated input
control unchanged. By default, the initial value is an empty string (""), which indicates that
a value must be entered in the input control for it to pass validation.

Note: Extra spaces at the beginning and end of the input value are removed before
validation is performed. This prevents a space being entered in the input control from
passing validation.

PROPERTIES

BackColor The background color of the RequiredFieldValidator control


ControlToValidate The id of the control to validate

Page 8 of 11
WEB PROG. USING ASP.NET
6th Semester VALIDATION CONTROLS Krunal Prajapati

Display The display behavior for the validation control. Legal values are:

 None (the control is not displayed. Used to show the error message only in
the ValidationSummary control)
 Static (the control displays an error message if validation fails. Space is
reserved on the page for the message even if the input passes validation.
 Dynamic (the control displays an error message if validation fails. Space is
not reserved on the page for the message if the input passes validation

EnableClientScript A Boolean value that specifies whether client-side validation is enabled or not
Enabled A Boolean value that specifies whether the validation control is enabled or not
ErrorMessage The text to display in the ValidationSummary control when validation
fails.Note: This text will also be displayed in the validation control if the Text
property is not set
ForeColor The foreground color of the control
id A unique id for the control
InitialValue Specifies the starting value of the input control. Default value is ""
IsValid A Boolean value that indicates whether the control specified by ControlToValidate
is determined to be valid
runat Specifies that the control is a server control. Must be set to "server"
Text The message to display when validation fails

EXAMPLE

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


<div>
UserName:
<asp:TextBox ID="TextBox1" runat="server" />
<br />
Password:
<asp:TextBox ID="TextBox2" runat="server" TextMode="Password" />
<br />
<asp:Button ID="Button1" runat="server" Text="Login" />
<br /><br />
<asp:RequiredFieldValidator
ID="RequiredFieldValidator1"
ControlToValidate="Textbox1"
runat="server"
ErrorMessage="You must enter UserName.!" />
</div>
</form>

OUTPUT

Page 9 of 11
WEB PROG. USING ASP.NET
6th Semester VALIDATION CONTROLS Krunal Prajapati

VALIDATION SUMMARY

 The ValidationSummary control allows you to summarize the error messages from all
validation controls on a Web page in a single location. The summary can be displayed as a
list, a bulleted list, or a single paragraph, based on the value of the DisplayMode property.
The error message displayed in the ValidationSummary control for each validation control
on the page is specified by the ErrorMessage property of each validation control. If
the ErrorMessage property of the validation control is not set, no error message is
displayed in the ValidationSummary control for that validation control. You can also
specify a custom title in the heading section of the ValidationSummarycontrol by setting
the HeaderText property.

PROPERTIES

DisplayMode How to display the summary. Legal values are:

 BulletList
 List
 SingleParagraph

EnableClientScript A Boolean value that specifies whether client-side validation is enabled or not
Enabled A Boolean value that specifies whether the validation control is enabled or not
ForeColor The fore color of the control
HeaderText A header in the ValidationSummary control
id A unique id for the control
runat Specifies that the control is a server control. Must be set to "server"
ShowMessageBox A Boolean value that specifies whether the summary should be displayed in a
message box or not
ShowSummary A Boolean value that specifies whether the ValidationSummary control should be
displayed or hidden

OUTPUT

Page 10 of 11
WEB PROG. USING ASP.NET
6th Semester VALIDATION CONTROLS Krunal Prajapati

EXAMPLE

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


<div>
<table>
<tr>
<td>
<table bgcolor="#b0c4de" cellspacing="10">
<tr>
<td align="right">Name:</td>
<td><asp:TextBox id="txt_name" runat="server"/></td>
<td>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1"
ControlToValidate="txt_name"
ErrorMessage="Name"
Text="*"
runat="server"/>
</td>
</tr>
<tr>
<td align="right">Card Type:</td>
<td>
<asp:RadioButtonList id="rlist_type"
RepeatLayout="Flow"
runat="server">
<asp:ListItem>Diners</asp:ListItem>
<asp:ListItem>MasterCard</asp:ListItem>
<asp:ListItem>Visa</asp:ListItem>
</asp:RadioButtonList>
</td>
<td>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2"
ControlToValidate="rlist_type"
ErrorMessage="Card Type"
InitialValue=""
Text="*"
runat="server"/>
</td>
</tr>
<tr>
<td></td>
<td><asp:Button id="b1" Text="Submit" runat="server"/></td>
<td></td>
</tr>
</table>
</td>
</tr>
</table><br />
<asp:ValidationSummary ID="ValidationSummary1"
HeaderText="You must enter a value in the following fields:"
EnableClientScript="true"
runat="server"/>
</div></form>

Page 11 of 11

You might also like