CustomValidator Control

The CustomValidator control allows developers to provide their own method to validate another control's field.

Mobile Controls Syntax

Required properties, defaults, and code-featured elements are noted in bold type.

<mobile:CustomValidator
   runat="server"
   id="id"
   Font-Name="fontName"
   Font-Size="{NotSet|Normal|Small|Large}"
   Font-Bold="{NotSet|False|True}"
   Font-Italic=""{NotSet|False|True}"
   ForeColor="foregroundColor"
   BackColor="backgroundColor"
   Alignment="{NotSet|Left|Center|Right}"
   StyleReference="styleReference"
   Text="ErrorText"
   Wrapping="{NotSet|Wrap|NoWrap}"

   ControlToValidate="IdOfTargetControl"
   Display="{None|Static|Dynamic}"
   ErrorMessage="ErrorTextForSummary"
   OnServerValidate="EventHandler">
innerText
</mobile:CustomValidator>

Containment Rules

The following controls can contain a CustomValidator control.

Control Comments
System.Web.UI.MobileControls.Form Can contain any number of CustomValidator controls.
System.Web.UI.MobileControls.Panel Can contain any number of CustomValidator controls.

The CustomValidator control cannot contain any other controls.

Device Templates

None.

Device-Specific Behavior

When any validator is rendered, the device-specific nature of its rendering is exactly the same as the device-specific rendering of a Label control. However, the StyleReference property for a validator is initially set to Error. It will use the error style in the page Stylesheet control or, if there is none, it will use the system default style sheet.

Example

The code in this example ensures that a number entered by a user is even.

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

See Also

CustomValidator Class | CustomValidator Class Members | CustomValidator Web Server Control | Control Reference