Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

IValidator Interface

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

For a list of all members of this type, see IValidator Members.

[Visual Basic]
Public Interface IValidator
[C#]
public interface IValidator
[C++]
public __gc __interface IValidator
[JScript]
public interface IValidator

Classes that Implement IValidator

Class Description
BaseValidator Serves as the abstract base class for validation controls.

Remarks

Classes that implement this interface represent a posssible 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 Web Forms Validation.

For details on how to develop custom ASP.NET validation server controls, see Developing a Validator Control. For code examples of custom ASP.NET validation server controls, see Validator Control Samples.

Example

[Visual Basic] 
<HTML>
   <HEAD>
      <script language="VB" runat="server">

         Sub Button_Click(sender As [Object], e As EventArgs)
            ' Generating a random number.
            Dim rand_number As New Random()
            myCompareValidate.ValueToCompare = rand_number.Next(1, 10).ToString()

            ' Set the ErrorMessage.
            myCompareValidate.ErrorMessage = "Try Again!!"
            myCompareValidate.Validate()

            ' Check for Validity of control.
            If myCompareValidate.IsValid And myTextBox.Text <> "" Then
               labelOutput.Text = "You guessed correctly!!"
               labelOutput.ForeColor = System.Drawing.Color.Blue
            Else
               labelOutput.Text = "You guessed poorly"
               labelOutput.ForeColor = System.Drawing.Color.Black
            End If

            labelOutput.Text += "<br><br>" + "The number is: " + _
               myCompareValidate.ValueToCompare
         End Sub '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 Text="Submit" OnClick="Button_Click" runat="server" />
          <br>
          <asp:Label id="labelOutput" runat="server" />
          <br>
          <asp:ValidationSummary id="Summary1" runat="server" />
       </form>
    </body>
</HTML>

[C#] 
<HTML>
  <HEAD>
    <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>

[C++, JScript] No example is available for C++ or JScript. To view a Visual Basic or C# example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.Web.UI

Platforms: Windows 2000, Windows XP Professional, Windows Server 2003 family

Assembly: System.Web (in System.Web.dll)

See Also

IValidator Members | System.Web.UI Namespace | ValidatorCollection | Validators | Developing a Validator Control | BaseValidator | BaseCompareValidator | CompareValidator | CustomValidator | RangeValidator | RegularExpressionValidator | RequiredFieldValidator | Web Forms Validation

Show:
© 2015 Microsoft