Validate Method
Collapse the table of content
Expand the table of content

IValidator.Validate Method

When implemented by a class, evaluates the condition it checks and updates the IsValid property.

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

void Validate()

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.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<html xmlns="" >
    <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_s = new Random();
        myCompareValidate.ValueToCompare = rand_s.Next(1, 10).ToString();

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

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

        labelOutput.Text += "<br /><br />" + "The number is: " +

      <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" />

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1
© 2015 Microsoft