Export (0) Print
Expand All
0 out of 2 rated this helpful - Rate this topic

RequiredFieldValidator Class

Makes the associated input control a required field.

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

System.Object
   System.Web.UI.Control
      System.Web.UI.WebControls.WebControl
         System.Web.UI.WebControls.Label
            System.Web.UI.WebControls.BaseValidator
               System.Web.UI.WebControls.RequiredFieldValidator

[Visual Basic]
Public Class RequiredFieldValidator
   Inherits BaseValidator
[C#]
public class RequiredFieldValidator : BaseValidator
[C++]
public __gc class RequiredFieldValidator : public BaseValidator
[JScript]
public class RequiredFieldValidator extends BaseValidator

Thread Safety

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

Remarks

Use this control to make an input control a required field. The input control fails validation if its value does not change from the InitialValue property upon losing focus.

Multiple validators can be associated with the same input control. For example, a RequiredFieldValidator can be used to ensure input to a control, while at the same time a RangeValidator can be used to ensure that the input is within a specified data range.

For additional information on validation controls, see BaseValidator.

Example

[Visual Basic, C#] The following example demonstrates how to use the RequiredFieldValidator control to make sure that the user enters a value into the text box.

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

   <script runat="server">
 
      Sub ValidateBtn_Click(sender As Object, e As EventArgs) 
 
         If Page.IsValid Then 
         
            lblOutput.Text = "Required field is filled!"
         
         Else 
         
            lblOutput.Text = "Required field is empty!"
         
         End If

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

      <h3>RequiredField Validator Example</h3>
 
      <table bgcolor="#eeeeee" cellpadding="10">
         <tr valign="top">
            <td colspan="3">
               <asp:Label ID="lblOutput" 
                    Text="Fill in the required field below"
                    runat="server"/>
               <br>
            </td>
         </tr>
 
         <tr>
            <td colspan="3">
               <b>Credit Card Information</b>
            </td>
         </tr>
     
         <tr>
            <td align="right">
               Card Number:
            </td>
            <td>
               <asp:TextBox id="TextBox1" 
                    runat="server"/>
            </td>
            <td>
               <asp:RequiredFieldValidator id="RequiredFieldValidator2"
                    ControlToValidate="TextBox1"
                    Display="Static"
                    ErrorMessage="*"
                    runat="server"/> 
            </td>
         </tr>
         <tr>
            <td></td>
            <td>
               <asp:Button id="Button1" 
                    Text="Validate" 
                    OnClick="ValidateBtn_Click" 
                    runat="server"/>
            </td>
            <td></td>
         </tr>
      </table>
 
   </form>
 
</body>
</html>
    

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

   <script runat="server">
 
      void ValidateBtn_Click(Object sender, EventArgs e) 
      {
 
         if (Page.IsValid) 
         {
            lblOutput.Text = "Required field is filled!";
         }
         else 
         {
            lblOutput.Text = "Required field is empty!";
         }
      }
 
   </script>
 
</head>
<body>
 
   <form runat="server">

      <h3>RequiredField Validator Example</h3>
 
      <table bgcolor="#eeeeee" cellpadding="10">
         <tr valign="top">
            <td colspan="3">
               <asp:Label ID="lblOutput" 
                    Text="Fill in the required field below"
                    runat="server"/>
               <br>
            </td>
         </tr>
 
         <tr>
            <td colspan="3">
               <b>Credit Card Information</b>
            </td>
         </tr>
     
         <tr>
            <td align="right">
               Card Number:
            </td>
            <td>
               <asp:TextBox id="TextBox1" 
                    runat="server"/>
            </td>
            <td>
               <asp:RequiredFieldValidator id="RequiredFieldValidator2"
                    ControlToValidate="TextBox1"
                    Display="Static"
                    ErrorMessage="*"
                    runat="server"/> 
            </td>
         </tr>
         <tr>
            <td></td>
            <td>
               <asp:Button id="Button1" 
                    Text="Validate" 
                    OnClick="ValidateBtn_Click" 
                    runat="server"/>
            </td>
            <td></td>
         </tr>
      </table>
 
   </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.WebControls

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

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

See Also

RequiredFieldValidator Members | System.Web.UI.WebControls Namespace | RangeValidator | InitialValue

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.