C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
ASP.NET RequiredFieldValidator ControlThis validator is used to make an input control required. It will throw an error if user leaves input control empty. It is used to mandate form control required and restrict the user to provide data. Note: It removes extra spaces from the beginning and end of the input value before validation is performed.The ControlToValidateproperty should be set with the ID of control to validate. RequiredFieldValidator Properties
ExampleHere, in the following example, we are explaining RequiredFieldValidator control and creating to mandatory TextBox controls. // RequiredFieldValidator.aspx <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="RequiredFieldValidator.aspx.cs" Inherits="asp.netexample.RequiredFieldValidator" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <style type="text/css"> .auto-style1 { width: 100%; } .auto-style2 { width: 165px; } </style> </head> <body> <form id="form1" runat="server"> <div> </div> <table class="auto-style1"> <tr> <td class="auto-style2">User Name</td> <td> <asp:TextBox ID="username" runat="server"></asp:TextBox> <asp:RequiredFieldValidatorID="user" runat="server" ControlToValidate="username" ErrorMessage="Please enter a user name" ForeColor="Red"></asp:RequiredFieldValidator> </td> </tr> <tr> <td class="auto-style2">Password</td> <td> <asp:TextBox ID="password" runat="server"></asp:TextBox> <asp:RequiredFieldValidator ID="pass" runat="server" ControlToValidate="password" ErrorMessage="Please enter a password" ForeColor="Red"></asp:RequiredFieldValidator> </td> </tr> <tr> <td class="auto-style2">�</td> <td> <br/> <asp:Button ID="Button1" runat="server" Text="login"/> </td> </tr> </table> </form> </body> </html> Output: It produces the following output when view in the browser. It throws error messages when user login with empty controls.
Next TopicASP.NET ValidationSummary
|