Professional Documents
Culture Documents
Home ASP.NET C# ADO.NET Java PHP HTML CSS JavaScript jQuery XML
https://www.javatpoint.com/asp-net-web-form-required-field-validator 1/9
22/11/2022, 06:24 ASP.Net Web Form Required Field Validator - javatpoint
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.
RequiredFieldValidator Properties
Property Description
ErrorMessage It is used to set error message that display when validation fails.
Example
https://www.javatpoint.com/asp-net-web-form-required-field-validator 2/9
22/11/2022, 06:24 ASP.Net Web Form Required Field Validator - javatpoint
Here, 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>
https://www.javatpoint.com/asp-net-web-form-required-field-validator 3/9
22/11/2022, 06:24 ASP.Net Web Form Required Field Validator - javatpoint
<asp:TextBox ID="username" runat="server"></asp:TextBox>
<asp:RequiredFieldValidatorIDasp:RequiredFieldValidatorID="user" runat="server" ControlToValid
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" ErrorMessa
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:
https://www.javatpoint.com/asp-net-web-form-required-field-validator 4/9
22/11/2022, 06:24 ASP.Net Web Form Required Field Validator - javatpoint
← Prev
Next →
Youtube
For Videos Join Our Youtube Channel: Join Now
Feedback
https://www.javatpoint.com/asp-net-web-form-required-field-validator 5/9
22/11/2022, 06:24 ASP.Net Web Form Required Field Validator - javatpoint
Preparation
Company
Interview
Questions
Company Questions
Trending Technologies
B.Tech / MCA
https://www.javatpoint.com/asp-net-web-form-required-field-validator 7/9
22/11/2022, 06:24 ASP.Net Web Form Required Field Validator - javatpoint
https://www.javatpoint.com/asp-net-web-form-required-field-validator 8/9
22/11/2022, 06:24 ASP.Net Web Form Required Field Validator - javatpoint
https://www.javatpoint.com/asp-net-web-form-required-field-validator 9/9