Export (0) Print
Expand All

RequiredFieldValidator Class

Makes the associated input control a required field.

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

[AspNetHostingPermissionAttribute(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
[AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
public class RequiredFieldValidator : BaseValidator
<asp:RequiredFieldValidator />

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.

NoteNote:

When you use the RequiredFieldValidator control inside an UpdatePanel control, make sure that the validator control and the control it is associated with are in the same panel. For more information about using the UpdatePanel control for partial-page updates, see Partial-Page Rendering Overview.

For additional information on validation controls, see BaseValidator.

Accessibility

The markup rendered by default for this control might not conform to accessibility standards such as the Web Content Accessibility Guidelines 1.0 (WCAG) priority 1 guidelines. For details about accessibility support for this control, see ASP.NET Controls and Accessibility.

TopicLocation
How to: Validate Against Values in a Database for ASP.NET Server ControlsBuilding ASP .NET Web Applications
How to: Validate Against a Range of Values for ASP.NET Server ControlsBuilding ASP .NET Web Applications
How to: Validate Against a Specific Value for ASP.NET Server ControlsBuilding ASP .NET Web Applications
How to: Validate Against a Data Type for ASP.NET Server ControlsBuilding ASP .NET Web Applications
How to: Specify Layout for In-Place Messages On ASP.NET Server ControlsBuilding ASP .NET Web Applications
How to: Validate Required Entries for ASP.NET Server ControlsBuilding ASP .NET Web Applications
How to: Display Server Side Custom Validation Messages for ASP.NET Server ControlsBuilding ASP .NET Web Applications
How to: Test Validity Programmatically for ASP.NET Server ControlsBuilding ASP .NET Web Applications
How to: Validate Against Patterns for ASP.NET Server ControlsBuilding ASP .NET Web Applications
How to: Validate Programmatically for ASP.NET Server ControlsBuilding ASP .NET Web Applications
How to: Format Validation Error Messages for ASP.NET Server ControlsBuilding ASP .NET Web Applications
How to: Validate with a Custom Function for ASP.NET Server ControlsBuilding ASP .NET Web Applications
How to: Disable Validation for ASP.NET Server ControlsBuilding ASP .NET Web Applications
How to: Validate Against Values in a Database for ASP.NET Server ControlsBuilding ASP .NET Web Applications
How to: Validate Against a Range of Values for ASP.NET Server ControlsBuilding ASP .NET Web Applications
How to: Validate Against a Specific Value for ASP.NET Server ControlsBuilding ASP .NET Web Applications
How to: Validate Against a Data Type for ASP.NET Server ControlsBuilding ASP .NET Web Applications
How to: Specify Layout for In-Place Messages On ASP.NET Server ControlsBuilding ASP .NET Web Applications
How to: Validate Required Entries for ASP.NET Server ControlsBuilding ASP .NET Web Applications
How to: Display Server Side Custom Validation Messages for ASP.NET Server ControlsBuilding ASP .NET Web Applications
How to: Test Validity Programmatically for ASP.NET Server ControlsBuilding ASP .NET Web Applications
How to: Validate Against Patterns for ASP.NET Server ControlsBuilding ASP .NET Web Applications
How to: Validate Programmatically for ASP.NET Server ControlsBuilding ASP .NET Web Applications
How to: Format Validation Error Messages for ASP.NET Server ControlsBuilding ASP .NET Web Applications
How to: Validate with a Custom Function for ASP.NET Server ControlsBuilding ASP .NET Web Applications
How to: Disable Validation for ASP.NET Server ControlsBuilding ASP .NET Web Applications
Walkthrough: Validating User Input in a Web Forms PageBuilding ASP .NET Web Applications in Visual Studio

The following example demonstrates how to use the RequiredFieldValidator control to make sure that the user enters a value into the text box.

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.

<%@ Page Language="C#" AutoEventWireup="True" %>
<!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>RequiredField Validator Example</title>
<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 id="form1" runat="server">

      <h3>RequiredField Validator Example</h3>

      <table style="background-color:#eeeeee; padding:10">
         <tr valign="top">
            <td colspan="3">
               <asp:Label ID="lblOutput" 
                    Text="Fill in the required field below"
                    runat="server"
                    AssociatedControlID="TextBox1"/>
               <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>
<html>
<head>
   <script language="C#" runat=server>

      void ValidateBtn_Click(Object sender, EventArgs e) {

         if (Page.IsValid == true) {
            lblOutput.Text = "Required field is filled!";
         }
         else {
            lblOutput.Text = "Required field is empty!";
         }
      }

   </script>

</head>
<body>

<h3><font face="Verdana">RequiredField Validator Sample</font></h3>
<p>

<form runat="server">

   <table bgcolor="#eeeeee" cellpadding=10>
      <tr valign="top">
         <td colspan=3>
            <asp:Label ID="lblOutput" 
                 Text="Fill in the required field below"
                 ForeColor="red" 
                 Font-Name="Verdana" 
                 Font-Size="10" runat=server /><br>
         </td>
      </tr>

      <tr>
         <td colspan=3>
            <font face=Verdana size=2><b>Credit Card Information</b></font>
         </td>
      </tr>

      <tr>
         <td align=right>
            <font face=Verdana size=2>Card Number:</font>
         </td>
         <td>
            <asp:TextBox id=TextBox1 runat=server />
         </td>
         <td>
            <asp:RequiredFieldValidator id="RequiredFieldValidator2"
                 ControlToValidate="TextBox1"
                 Display="Static"
                 Width="100%" runat=server>
                 *
            </asp:RequiredFieldValidator>

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

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

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

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft