RequiredFieldValidator example in Asp.Net

InfoPath alternatives for form designing SharePoint

In this post, we will see an example of how to use RequiredFieldValidator in Asp.Net.

There are different types of validation controls are provided by Asp.Net.

– RequiredFieldValidator will show an error message if a user tries to submit without entering a value in the field.

– To use RequiredFieldValidator, drag and drop a RequiredFieldValidator from Toolbox.

Here ControlToValidate property field needs to be set as on which field you want to validate. And ErrorMessage property will show whatever error message you will give.

Below is the full HTML code

<%@ Page Language=”C#” AutoEventWireup=”true” CodeFile=”ValidatorExamples.aspx.cs”
Inherits=”ValidatorExamples” %>

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head runat=”server”>
<title>RequiredFieldValidator example in Asp.Net</title>
</head>
<body>
<form id=”form1″ runat=”server”>
<div>
Enter First Name:<asp:TextBox ID=”txtFirstName” runat=”server”></asp:TextBox><br />
<asp:RequiredFieldValidator ID=”RequiredFieldValidator1″ runat=”server” ForeColor=”Red” ErrorMessage=”Enter First Name!”
ControlToValidate=”txtFirstName”></asp:RequiredFieldValidator><br />
<asp:Button ID=”btnSubmit” runat=”server” Text=”Submit” />
</div>
</form>
</body>
</html>


If you try to submit enter without filling any value in the Textbox then you will get the error message as shown in the figure below:

Also, you can check out my previous posts on:

You May Also like the Following SharePoint Online Tutorials:

About Bijay Kumar

I am Bijay from Odisha, India. Currently working in my own venture TSInfo Technologies in Bangalore, India. I am Microsoft Office Servers and Services (SharePoint) MVP (5 times). I works in SharePoint 2016/2013/2010, SharePoint Online Office 365 etc. Check out My MVP Profile.. I also run popular SharePoint web site SharePointSky.com

View all posts by Bijay Kumar →

Leave a Reply