- CompareFieldValidator Web Server Control
in ASP.NET environment,This article describes you how to work with CompareValidator Web Control.This CompareValidator can be used to comparing the entered value of 2 controls.It Compares a user's entry against a constant value, against the value of another control or for a specific data type. For example, For comparing both the Password and ConfirmPassword that can be done by CompareFieldValidator Control.
The major purspose of this control is to Compares the value entered by the user in an input control with the value entered in another input control, or with a constant value.Specify the input control to validate by setting the ControlToValidate property. If you want to compare a specific input control with another input control, set the ControlToCompare property to specify the control to compare with.
ASP.NET CompareValidator using the Type property to specify the data type of both comparison values. Both values are automatically converted to this data type before the comparison operation is performed. The following various data types that can be compared.
- String
- Integer
- Double
- Date
- Currency
The following code snippets explains you the RequiredFieldValidator
-
<asp:CompareValidator id="Compare1"
ControlToValidate="txt1"
ControlToCompare="txt2"
EnableClientScript="False"
Type="String"
runat="server"/>
-
- Formating the CompareFieldValidator
ASP.NET validation controls allows you to customizing the format—font, size, and so on—of error text, or you can substitute a marker for error text. For example, you can have the validation control display an asterisk (*) when an error occurs.
You can also include a detailed error message in the ErrorMessage property of the validation control and add a ValidationSummary control to the page. The detailed ErrorMessage property text will appear on the page in the location of the ValidationSummary control.
To format error messages, specify the following properties,
- ForeColor - The color of the error message text.
- BackColor -The color behind the text.
- Font -The font face, size, weight, and so on.
- BorderWidth, BorderColor, and BorderStyle -The size and color of a border around the error message.
The following example demonstrates how to use the RequiredFieldValidator control to make sure that the user enters a value into the text box.
-
in .aspx.cs page,
-
void Button_Click(Object sender, EventArgs e)
{
if (Page.IsValid)
{
lblOutput.Text = "both are same!";
}
else
{
lblOutput.Text = "both are not same!";
}
}
void Operator_Index_Changed(Object sender, EventArgs e)
{
Compare1.Operator = (ValidationCompareOperator) ListOperator.SelectedIndex;
Compare1.Validate();
}
void Type_Index_Changed(Object sender, EventArgs e)
{
Compare1.Type = (ValidationDataType) ListType.SelectedIndex;
Compare1.Validate();
}
-
in .aspx page,
-
<form id="Form2" runat=server>
<h3>CompareValidator Example</h3>
<p>
Enter a value in each textbox. Select a comparison operator<br>
and data type. Click "Validate" to compare values.
<table bgcolor="#eeeeee" cellpadding=10>
<tr valign="top">
<td>
<h5>String 1:</h5>
<asp:TextBox id="txt1"
runat="server"/>
</td>
<td>
<h5>Comparison Operator:</h5>
<asp:ListBox id="ListOperator"
OnSelectedIndexChanged="Operator_Index_Changed"
runat="server">
<asp:ListItem Selected Value="same">Equal</asp:ListItem>
<asp:ListItem Value="notsame">NotEqual</asp:ListItem>
</asp:ListBox>
</td>
<td>
<h5>String 2:</h5>
<asp:TextBox id="txt2"
runat="server"/>
<p>
<asp:Button id="btn1"
Text="Validate"
OnClick="Button_Click"
runat="server"/>
</td>
</tr>
<tr>
<td colspan="3" align="center">
<h5>Data Type:</h5>
<asp:ListBox id="ListType"
OnSelectedIndexChanged="Type_Index_Changed"
runat="server">
<asp:ListItem Selected Value="String" >String</asp:ListItem>
<asp:ListItem Value="Integer" >Integer</asp:ListItem>
<asp:ListItem Value="Double" >Double</asp:ListItem>
<asp:ListItem Value="Date" >Date</asp:ListItem>
<asp:ListItem Value="Currency" >Currency</asp:ListItem>
</asp:ListBox>
</td>
</tr>
</table>
<asp:CompareValidator id="Compare1"
ControlToValidate="txt1"
ControlToCompare="txt2"
EnableClientScript="False"
Type="String"
runat="server"/>
<br>
<asp:Label id="lblOutput"
Font-Name="verdana"
Font-Size="10pt"
runat="server"/>
</form>
-
- Related Links
-
d3758c5a-5300-4499-af52-53f1399a331d|0|.0
592591bc-3062-42b8-a79f-bd28eb8d09aa|0|.0