CustomValidator Class

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

Namespace: System.Web.UI.MobileControls
Assembly: System.Web.Mobile (in

public class CustomValidator : BaseValidator
public class CustomValidator extends BaseValidator
public class CustomValidator extends BaseValidator

This class behaves identically to the System.Web.UI.WebControls.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 events 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.

The following 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 ErrorMessage property.


The following code sample uses the single-file code model and may not work correctly if copied directly into a code-behind file. This code sample must be copied into an empty text file that has an .aspx extension. For more information on the Web Forms code model, see ASP.NET Web Page Code Model.

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

<script runat="server">
    // If the page validates, go to page 2
    protected void Submit_Click(Object sender, EventArgs e)
        if (Page.IsValid)
            ActiveForm = Form2;
    // Validate whether the number is even
    private void ServerValidate(object source, 
        ServerValidateEventArgs args)
        // Convert the text to a number
        int num;
        Int32.TryParse(numberBox.Text, out num);
        // Test for an even number
        if (num > 0)
            args.IsValid = ((num % 2) == 0);
            args.IsValid = false;

<html xmlns="" >
    <mobile:form id="Form1" runat="server">
        <mobile:Label ID="Label1" runat="server">
            Please enter an even number greater than zero.
        <mobile:TextBox ID="numberBox" Runat="server" 
            Numeric="true" MaxLength="2" />
        <mobile:CustomValidator ID="CustomValidator1" 
            OnServerValidate="ServerValidate" runat="server">
            Your number is not an even number.
        <mobile:Command ID="Command1" runat="server" 
    <mobile:Form id="Form2" runat="server">
        <mobile:Label ID="Label2" runat="server">
            Your number is an even number.

  • AspNetHostingPermission  for operating in a hosted environment. Demand value: LinkDemand; Permission value: Minimal.
  • AspNetHostingPermission  for operating in a hosted environment. Demand value: InheritanceDemand; Permission value: Minimal.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1

Community Additions