Introduction to ASP.NET RequiredFieldValidator
A RequiredFieldValidator server control is a control used to make a form field mandatory for user input. It restricts the user to post the form data unless the required field has been filled with some value. An error is thrown if the user leaves the required field blank. Thus, an application developer can enforce a validation policy on the web page and prevent posting the form data to the server unless the validation check has passed. ASP.NET provides its own tag for the RequiredFieldValidator control which is run at the server and the generated HTML code is returned as a response to the browser.
How Does the RequiredFieldValidator Mechanism Work?
The RequiredFieldValidator control can be coded using ASP.NET provided tags or dragged and dropped using Visual Studio IDE. The drag and drop feature ultimately generates the same code.
The syntax of ASP.NET RequiredFieldValidator in its simplest form is:
Syntax
<asp:RequiredFieldValidator
ID="RequiredFieldValidatorId" runat="server"
ControlToValidate="TargetControlId"
ErrorMessage="ErrorMessage">
</asp:RequiredFieldValidator>
The above mentioned are the basic required properties of RequiredFieldValidator control.
Let’s understand what each property signifies:
- ID: A unique Id of the RequiredFieldValidator Control.
- ControlToValidate: The Id of the control which is required to be filled in by the user. This is a mandatory property.
- ErrorMessage: The message to be displayed when validation fails.
You can drag and drop the RequiredFieldValidator control from the toolbox pane in Visual Studio:
This generates the below code in the aspx page:
Alternatively, you can manually write the code as well:
Code:
<asp:RequiredFieldValidator
ID="RequiredFieldValidator1"
runat="server"
ErrorMessage="RequiredFieldValidator">
</asp:RequiredFieldValidator>
Behind the Scenes
So, how does the RequiredFieldValidator mechanism work? When the ASP.NET engine encounters the control, it adds a task in the checklist before posting the form to the server. The validation is triggered whenever a user tries to post the form data to the server. If the validation fails, an error message is displayed and the server call is not made. The validation check must pass in order to post the form data to the server.
Properties
The ASP.NET RequiredFieldValidator control comes with certain pre-defined properties. These properties are converted to attributes in the native HTML code. They help define additional behavior for the RequiredFieldValidator control.
Let’s discuss some of the frequently used ones in detail:
1. BackColor, ForeColor
This property gets or sets the background and the foreground color of the control.
<asp:RequiredFieldValidator ID="myRequiredFieldValidator" ControlToValidate="TextBox_1" ErrorMessage="Error" BackColor="DarkBlue" ForeColor="White" runat="server"> </asp:RequiredFieldValidator>
2. BorderColor, BorderStyle and BorderWidth
These properties get or set the border styling for the control.
<asp:RequiredFieldValidator ID="myRequiredFieldValidator"” ControlToValidate="TextBox_1" ErrorMessage="Error" BorderWidth="5" BorderColor="Blue" BorderStyle="dashed" runat="server"> </asp:RequiredFieldValidator>
3. CssClass
This property gets or sets the CSS class to be applied to the control.
<asp:RequiredFieldValidator ID="myRequiredFieldValidator" ControlToValidate="TextBox_1" ErrorMessage="Error" CssClass="txtBxClass" runat="server"> </asp:RequiredFieldValidator>
4. Enabled
This property gets or sets the value indicating whether the control is enabled or disabled. The default value is true.
<asp:RequiredFieldValidator ID="myRequiredFieldValidator" ControlToValidate="TextBox_1" ErrorMessage="Error" Enabled="false" runat="server"> </asp:RequiredFieldValidator>
5. EnableClientScript
This property gets or sets the value indicating whether client-side scripting is enabled or disabled. When enabled, the validation checks are performed at the browser itself before posting data to the server. When disabled, the validation checks are performed at the server and error is returned in the response if validation fails. The default value is true.
<asp:RequiredFieldValidator ID="myRequiredFieldValidator" ControlToValidate="TextBox_1" ErrorMessage="Error" EnableClientScript="false" runat="server"> </asp:RequiredFieldValidator>
6. Font
This property gets or sets the font of the text to be displayed in the control. There are plenty of styles and options such as bold, italics, underline, strikeout, etc.
7. Height, Width
These properties get or set the height and width of the control in the number of pixels.
<asp:RequiredFieldValidator ID="myRequiredFieldValidator" ControlToValidate="TextBox_1" ErrorMessage="Error" Height="100" Width="500" runat="server"> </asp:RequiredFieldValidator>
8. ID
This property gets or sets the unique identifier attribute to the control.
<asp:RequiredFieldValidator ID="myRequiredFieldValidator" ControlToValidate="TextBox_1" ErrorMessage="Error" runat="server"> </asp:RequiredFieldValidator>
9. Visible
This property determines whether the control will be displayed on the UI or hidden. The default is true.
<asp:RequiredFieldValidator ID="myRequiredFieldValidator" ControlToValidate="TextBox_1" ErrorMessage="Error" Visible="false" runat="server"> </asp:RequiredFieldValidator>
Example to Implement RequiredFieldValidator
Let us create a RequiredFieldValidator sample application step by step.
Step 1. Create a new ASP.NET WebApplication project. This will create a shell template with a working application with a Default.aspx and Default.aspx.cs page. The .cs page is the code behind page for the .aspx page.
Step 2. Go to the Default.aspx file and remove the contents of the shell template. Paste the following code:
Code:
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master"
AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="SampleWebApplication._Default" %>
<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
<br /><br />
<asp:Label ID="email_lbl" runat="server">Enter Email ID</asp:Label>
<asp:TextBox ID="email_txt" TextMode="Email" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="Email_Required" runat="server"
ControlToValidate="email_txt"
ForeColor="DarkRed"
Font-Bold="true"
ErrorMessage="Please enter an Email ID.">
</asp:RequiredFieldValidator>
<br /><br />
<asp:Label ID="pass_lbl" runat="server">Enter Password</asp:Label>
<asp:TextBox ID="pass_txt" TextMode="Password" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="Pass_Required" runat="server"
ControlToValidate="pass_txt"
ForeColor="DarkRed"
Font-Italic="true"
ErrorMessage="Please enter the Password.">
</asp:RequiredFieldValidator>
<br /><br />
<asp:Label ID="msg" runat="server" Text=""></asp:Label>
<br /><br />
<asp:Button ID="Validate_btn" runat="server" OnClick="Validate_btn_Click" Text="Validate" />
</asp:Content>
Step 3. Copy the below code in your Default.aspx.cs file.
Code:
using System;
using System.Web.UI;
namespace SampleWebApplication
{
public partial class _Default : Page
{
protected void Validate_btn_Click(object sender, EventArgs e)
{
// This check is useful when client side scripting is disabled.
// It checks page validations on server to prevent any harmful data being posted.
if (Page.IsValid) {
msg.Text = "Account created successfully.";
}
}
}
}
Step 4. Run the application. Below is the output of your code. It has two textboxes and respective validation controls to check whether the values of both the text boxes have been filled or not.
- The code-behind file has a button click function to the Validate button. This method is called only when the RequiredFieldValidator results in a pass.
- The above is the landing page of your application. Click on the Validate button without entering any value in the fields. A validation error is thrown, and no call is made to the code behind the function.
- When the values are entered, the error disappears.
- Now click on the Validate button and the code behind the function is executed.
Conclusion
Voila! You have successfully learned the ASP.NET RequiredFieldValidator control. To get more advanced training on some of the advanced properties and code behind logics, the official Microsoft documentation is highly recommended.
Recommended Articles
We hope that this EDUCBA information on “ASP.NET RequiredFieldValidator” was beneficial to you. You can view EDUCBA’s recommended articles for more information.