Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

IValidator Interface

Defines the properties and methods that objects that participate in Web Forms validation must implement.

Namespace: System.Web.UI
Assembly: System.Web (in system.web.dll)
public interface IValidator
public interface IValidator
public interface IValidator
Not applicable.

Classes that implement this interface represent a possible user input error. When the Validate method is called, the class updates its IsValid property to signify whether the error occurred. The ErrorMessage property contains a text description of the error condition that you can display when the error occurs.

The BaseValidator class implements this interface, and all other ASP.NET validation server control classes inherit from BaseValidator. For information on validation server controls and how they work, see Validation ASP.NET Controls.

For details on how to develop custom ASP.NET validation server controls, see How to: Validate with a Custom Function for ASP.NET Server Controls.

Security noteSecurity Note:

This example has a text box that accepts user input, which is a potential security threat. By default, ASP.NET Web pages validate that user input does not include script or HTML elements. For more information, see Script Exploits Overview (Visual Studio).

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
  <head>
    <title>IValidator Example demonstrating IsValid & ErrorMessage</title>
<script language="C#" runat="server">

     void Button_Click(Object sender, EventArgs e)
     {
        // Generating the random number.
        Random rand_number = new Random();
        myCompareValidate.ValueToCompare = rand_number.Next(1, 10).ToString();

        // Setting the ErrorMessage.
        myCompareValidate.ErrorMessage="Try Again!!";
        myCompareValidate.Validate();

        // Check for Validity of control.
        if ((myCompareValidate.IsValid) && (myTextBox.Text != ""))
        {
           labelOutput.Text = "You guessed correctly!!";
           labelOutput.ForeColor = System.Drawing.Color.Blue;
        }
        else
        {
           labelOutput.Text =  "You guessed poorly";
           labelOutput.ForeColor = System.Drawing.Color.Black;
        }

        labelOutput.Text += "<br /><br />" + "The number is: " +
           myCompareValidate.ValueToCompare;
     }

  </script>
</head>
    <body>
      <form runat="server" id="myForm">
        <h3>IValidator Example demonstrating IsValid & ErrorMessage</h3>
        <h5>Guess!! a number between 1 and 10 :</h5>
        <asp:TextBox id="myTextBox" runat="server" />
        <asp:CompareValidator id="myCompareValidate"
             ControlToValidate="myTextBox" ValueToCompare="0"
             EnableClientScript="False" Type="Integer" Text="*"
             runat="server" />
        <br />
        <asp:Button Text="Submit" OnClick="Button_Click" runat="server" />
        <br />
        <asp:Label id="labelOutput" runat="server" />
        <br />
        <asp:ValidationSummary id="Summary1" runat="server" />
     </form>
  </body>
</html>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
  <head>
    <title>IValidator Example demonstrating IsValid & ErrorMessage</title>
<script language="VJ#" runat="server">

    void Button_Click(Object sender, EventArgs e)
    {
        // Generating the random number.
        Random randNumber = new Random();
        myCompareValidate.set_ValueToCompare(
            System.Convert.ToString(randNumber.Next(1, 10)));

        // Setting the ErrorMessage.
        myCompareValidate.set_ErrorMessage("Try Again!!");
        myCompareValidate.Validate();

        // Check for Validity of control.
        if (myCompareValidate.get_IsValid() 
            && (!(myTextBox.get_Text().Equals("")))) {
            labelOutput.set_Text("You guessed correctly!!");
            labelOutput.set_ForeColor(System.Drawing.Color.get_Blue());
        }
        else {
            labelOutput.set_Text("You guessed poorly");
            labelOutput.set_ForeColor(System.Drawing.Color.get_Black());
        }
        labelOutput.set_Text(labelOutput.get_Text() + "<br /><br />" 
            + "The number is: " + myCompareValidate.get_ValueToCompare());
    } //Button_Click

  </script>
</head>
    <body>
      <form runat="server" id="myForm">
        <h3>IValidator Example demonstrating IsValid & ErrorMessage</h3>
        <h5>Guess!! a number between 1 and 10 :</h5>
        <asp:TextBox id="myTextBox" runat="server" />
        <asp:CompareValidator id="myCompareValidate"
             ControlToValidate="myTextBox" ValueToCompare="0"
             EnableClientScript="False" Type="Integer" Text="*"
             runat="server" />
        <br />
        <asp:Button ID="Button1" Text="Submit" OnClick="Button_Click" runat="server" />
        <br />
        <asp:Label id="labelOutput" runat="server" />
        <br />
        <asp:ValidationSummary id="Summary1" runat="server" />
     </form>
  </body>
</html>

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

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.