Public Class RequiredFieldValidator Inherits BaseValidator
Dim instance As RequiredFieldValidator
public class RequiredFieldValidator : BaseValidator
public ref class RequiredFieldValidator : public BaseValidator
public class RequiredFieldValidator extends BaseValidator
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.
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.
The following example demonstrates how to use the RequiredFieldValidator control to make sure that the user enters a value into the text box.
<%@ 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>
<%@ 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>
<html> <head> <script language="C#" runat=server> </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>
Windows 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.