IsValid Property

BaseValidator.IsValid Property

Gets or sets a value that indicates whether the associated input control passes validation.

[Visual Basic]
Public Overridable Property IsValid As Boolean  Implements _
   IValidator.IsValid
[C#]
public virtual bool IsValid {get; set;}
[C++]
public: __property virtual bool get_IsValid();
public: __property virtual void set_IsValid(bool);
[JScript]
public function get IsValid() : Boolean;
public function set IsValid(Boolean);

Property Value

true if the associated input control passes validation; otherwise, false. The default value is true.

Implements

IValidator.IsValid

Remarks

Use the IsValid property to determine whether the associated input control passes validation.

CAUTION   Since the default value of this property is true, it will return true if you query this property before validation is performed. For example, this might occur if you attempt to use this property in the Control.Load event of page.

The IsValid property is only evaluated when the Validate method is called. You can call the Validate method for each validation control on the page individually, or call all of them at once by using the Page.Validate method. Button controls with their CausesValidation property set to true will also call the Page.Validate method.

Note   It is possible to change the value of this property manually after validation has taken place. This allows you to override the validation result, if necessary.

The Page.IsValid property for the page is set to true only if the IsValid property for each validation control on the page is also set to true.

Example

[Visual Basic, C#] The following example demonstrates how to use the IsValid property to determine whether the associated input control passes validation.

[Visual Basic] 
<%@ Page Language="VB" AutoEventWireup="True" %>

<html>
<head>
   <script runat="server">
 
      Sub Button_Click(sender As Object, e As EventArgs) 

         Dim rand_number As Random = New Random()

         Compare1.ValueToCompare = rand_number.Next(1, 10).ToString()
         Compare1.Validate()
 
         If Compare1.IsValid And Require1.IsValid Then 

            lblOutput.Text = "You guessed correctly!!"
         
         Else 
         
            lblOutput.Text = "You guessed poorly"
         
         End If

         lblOutput.Text += "<br><br>" & "The number is: " & Compare1.ValueToCompare

      End Sub
 
   </script>
 
</head>
<body>
 
   <form runat=server>

      <h3>Validator Example</h3>

      <h5>Pick a number between 1 and 10:</h5>
     
      <asp:TextBox id="TextBox1" 
           runat="server"/>

      <asp:CompareValidator id="Compare1" 
           ControlToValidate="TextBox1"
           ValueToCompare="0"
           EnableClientScript="False"  
           Type="Integer"
           Display="Dynamic" 
           ErrorMessage="Incorrect guess!!"
           Text="*"
           runat="server"/>

      <asp:RequiredFieldValidator id="Require1" 
           ControlToValidate="TextBox1"
           EnableClientScript="False"
           Display="Dynamic" 
           ErrorMessage="No number entered!!"
           Text="*"
           runat="server"/>

      <br><br>

      <asp:Button id="Button1"
           Text="Submit"
           OnClick="Button_Click"
           runat="server"/>
 
      <br><br>
       
      <asp:Label id="lblOutput" 
           Font-Name="verdana" 
           Font-Size="10pt" 
           runat="server"/>

      <br><br>

      <asp:ValidationSummary
           id="Summary1"
           runat="server"/>
 
   </form>
 
</body>
</html>


[C#] 
<%@ Page Language="C#" AutoEventWireup="True" %>

<html>
<head>
   <script runat="server">
 
      void Button_Click(Object sender, EventArgs e) 
      {

         Random rand_number = new Random();

         Compare1.ValueToCompare = rand_number.Next(1, 10).ToString();
         Compare1.Validate();
 
         if (Compare1.IsValid && Require1.IsValid) 
         {
            lblOutput.Text = "You guessed correctly!!";
         }
         else 
         {
            lblOutput.Text = "You guessed poorly";
         }

         lblOutput.Text += "<br><br>" + "The number is: " + Compare1.ValueToCompare;

      }
 
   </script>
 
</head>
<body>
 
   <form runat=server>

      <h3>Validator Example</h3>

      <h5>Pick a number between 1 and 10:</h5>
     
      <asp:TextBox id="TextBox1" 
           runat="server"/>

      <asp:CompareValidator id="Compare1" 
           ControlToValidate="TextBox1"
           ValueToCompare="0"
           EnableClientScript="False"  
           Type="Integer"
           Display="Dynamic" 
           ErrorMessage="Incorrect guess!!"
           Text="*"
           runat="server"/>

      <asp:RequiredFieldValidator id="Require1" 
           ControlToValidate="TextBox1"
           EnableClientScript="False"
           Display="Dynamic" 
           ErrorMessage="No number entered!!"
           Text="*"
           runat="server"/>

      <br><br>

      <asp:Button id="Button1"
           Text="Submit"
           OnClick="Button_Click"
           runat="server"/>
 
      <br><br>
       
      <asp:Label id="lblOutput" 
           Font-Name="verdana" 
           Font-Size="10pt" 
           runat="server"/>

      <br><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

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

See Also

BaseValidator Class | BaseValidator Members | System.Web.UI.WebControls Namespace | ControlToValidate | Control.Load | Validate | Page.Validate | IsValid | Page.IsValid

Show:
© 2016 Microsoft