Introduction:

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

Description:

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:

RequiredFieldValidator.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="RequiredFieldValidator.aspx.cs"
 
Inherits="AspDotNet.RequiredFieldValidator" %>
 
<!DOCTYPE html>
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    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" />
    </div>
    </form>
</body>
</html>

 

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.

 

 

RequiredFieldValidator.aspx.cs:

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)
            {
            }
        }
    }
}

 

Output:



RequiredFieldValidator properties

Property

Description

ControlToValidate

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.

0 comments


Leave a comment

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

Welcome to SQL Tutorials Point

Don't have an account? Click here to Create a new account, it takes less than a minute.