In this article we will learn how to use RequiredFieldValidator in or what is RequiredFieldValidator in or why use RequiredFieldValidator in or how to make a required field in ASP.Net with example.


The RequiredFieldValidator  enable  to You can specify that a user must provide information in a specific control on an ASP.NET Web page. For example, you can specify that users must fill in a Name text box before they can submit a registration form by using  RequiredFieldValidator control and link to the Name text box  . If user submit the registration form without filling Name text box   the RequiredFieldValidator give error message.

Let’s see Example:


<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="RequiredFieldValidator.aspx.cs"
Inherits="AspDotNet.RequiredFieldValidator" %>
<!DOCTYPE html>
<html xmlns="">
<head runat="server">
    <form id="form1" runat="server">
    First Name: <asp:TextBox runat="server" id="txtfName" />
    <asp:RequiredFieldValidator runat="server" id="reqfName" Style="color:red;"
  controltovalidate="txtfName" errormessage="Please enter your First!" />
    <br /><br />
    Last Name: <asp:TextBox runat="server" id="txtLName" />
    <asp:RequiredFieldValidator runat="server" id="reqlname" Style="color:red;"
  controltovalidate="txtLName" errormessage="Please enter your Last Name!" />
    <br /><br />
    <asp:Button runat="server" id="btnSubmit" text="Submit" OnClick="btnSubmit_Click" />


If you want to check server side validation when user submit the form in that case you need to add enableclientscript="false" in  RequiredFieldValidator controls and then submit the form. In the server side Page.IsValid property tell us page is valid or not if  page  is not valid then give error message.




using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace AspDotNet
    public partial class RequiredFieldValidator : System.Web.UI.Page
        protected void Page_Load(object sender, EventArgs e)
        protected void btnSubmit_Click(object sender, EventArgs e)
            if (Page.IsValid)



How to use RequiredFieldValidator in Asp.Net

RequiredFieldValidator properties




The ID of the control for which the user must provide a value.

ErrorMessage , TextDisplay

Properties that specify the text and location of the error or errors that will appear if the user skips the control. For details, see How to: Control Validation Error Message Display for ASP.NET Server Controls.


I hope this article will help to you.

Leave a comment

Make sure you enter the (*) required information where indicated. HTML code is not allowed.

You may also like