CompareValidator Class

Determines validity by comparing a specific field in one control to a specific field in another control, using a specifiable comparison operator.

public class System.Web.UI.MobileControls.CompareValidator : 
   System.Web.UI.MobileControls.BaseValidator

Remarks

Use compare validators to ensure that the values of two text boxes provide the same values, such as confirming a new password.

This class behaves identically to the ASP.NET System.Web.UI.WebControls.CompareValidator control.

By default, Command controls on a form raise any validator controls on the form to perform validation when the form is submitted to the server. To disable automatic validation, set the CausesValidation property on the Command controls to False.

Validation succeeds if the input control is empty. Use a RequiredFieldValidator control to require the user to enter data into the input control.

Example

The following example code uses a CompareValidator control (CompareValidator1) to check whether the two text boxes have the same value and alerts the user if they are different. Those settings are provided in the page load portion of the example.

<%@ Page Inherits="System.Web.UI.MobileControls.MobilePage" 
   Language="VB" %>
<%@ Register TagPrefix="mobile"
    Namespace="System.Web.UI.MobileControls"
    Assembly="System.Web.Mobile" %>

<script Language="VB" runat=server>

Public Sub Page_Load(sender As Object, e As EventArgs)
   CompareValidator1.ErrorMessage = "Values are different"
   CompareValidator1.ControlToCompare = "TextBox1"
   CompareValidator1.ControlToValidate = "TextBox2"
End Sub
</script>

<mobile:Form id="Form1" runat=server>
   <mobile:Label id="Label1" runat=server>Enter values
      </mobile:label> 
   <mobile:TextBox id="TextBox1" runat="server" Text="abc" />
   <mobile:TextBox id="TextBox2" runat="server" Text="xyz" />
   <mobile:Command id="Command1" runat="server" Text="Ok" />
   <mobile:CompareValidator id="CompareValidator1" runat="server" />
</mobile:Form>
[C#]
<%@ Page Inherits="System.Web.UI.MobileControls.MobilePage" 
   Language="c#" %>
<%@ Register TagPrefix="mobile"
    Namespace="System.Web.UI.MobileControls"
    Assembly="System.Web.Mobile" %>

<script Language="c#" runat=server>

public void Page_Load(Object sender, EventArgs e)
{
   CompareValidator1.ErrorMessage = "Values are different";
   CompareValidator1.ControlToCompare = "TextBox1";
   CompareValidator1.ControlToValidate = "TextBox2";
}
</script>

<mobile:Form id="Form1" runat=server>
   <mobile:Label id="Label1" runat=server>Enter values
      </mobile:label> 
   <mobile:TextBox id="TextBox1" runat="server" Text="abc" />
   <mobile:TextBox id="TextBox2" runat="server" Text="xyz" />
   <mobile:Command id="Command1" runat="server" Text="Ok" />
   <mobile:CompareValidator id="CompareValidator1" runat="server" />
</mobile:Form>

Requirements

Namespace: System.Web.UI.MobileControls

Assembly: System.Web.Mobile

See Also

CompareValidator Control | BaseValidator | CustomValidator | RangeValidator | RegularExpressionValidator | RequiredFieldValidator | ValidationSummary