CustomValidator Class

Provides a control that can perform custom validation against another control.

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

Remarks

This class behaves identically to the CustomValidator Web server control. Developers can choose their own common language runtime delegate to use for validation.

By default, Command controls on a form raise 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.

Example

This example checks whether the value that a user places into the TextBox control is an even number. If the value is an even number, then the page is valid. If not, the page is not valid, and the CustomValidator displays the Text property.

<%@ 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">

protected Sub Submit_Click(sender As Object, e As EventArgs)
  If Page.IsValid Then
   ActiveForm = Form2
  End If
End Sub

Sub ServerValidate (source As object, args As ServerValidateEventArgs)
  Dim num as Int32
  num = Int32.Parse(number.Text)
  args.IsValid = ((num Mod 2) = 0)
End Sub
</script>

<mobile:Form id="Form1" runat="server">
  <mobile:Label runat="server">
   Please enter an even number.
  </mobile:Label>
  
  <mobile:TextBox id="number" runat="server"/>

  <mobile:CustomValidator ControlToValidate="number"
         OnServerValidate="ServerValidate"
         runat="server">
   Invalid number
  </mobile:CustomValidator>

  <mobile:Command runat="server" OnClick="Submit_Click">
   Submit
  </mobile:Command>

</mobile:Form>

<mobile:Form id="Form2" runat="server">
  <mobile:Label runat="server">number is submitted</mobile:Label>
</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">

protected void Submit_Click(Object sender, EventArgs e)
{
  if (Page.IsValid)
  {
   ActiveForm = Form2;
  }
}

void
ServerValidate (object source, ServerValidateEventArgs args)
{
   int num = Int32.Parse(number.Text);
   args.IsValid = ((num % 2) == 0);
}

</script>

<mobile:Form id="Form1" runat="server">
  <mobile:Label runat="server">
   Please enter an even number.
  </mobile:Label>
  
  <mobile:TextBox id="number" runat="server"/>

  <mobile:CustomValidator ControlToValidate="number"
         OnServerValidate="ServerValidate"
         runat="server">
   Invalid number
  </mobile:CustomValidator>

  <mobile:Command runat="server" OnClick="Submit_Click">
   Submit
  </mobile:Command>

</mobile:Form>

<mobile:Form id="Form2" runat="server">
  <mobile:Label runat="server">number is submitted</mobile:Label>
</mobile:Form>

Requirements

Namespace: System.Web.UI.MobileControls

Assembly: System.Web.Mobile

See Also

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