articles

RequiredFieldValidator Control in ASP.Net

Pushpendra Singh10331 09-Nov-2010

The RequiredFieldValidator control is used to make an input control a required field. With this control, the validation fails if the input value does not change from its initial value. By default, the initial value is an empty string ("").

<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="RequiredFieldValidator"></asp:RequiredFieldValidator>

 

Properties:

Property

Description

BackColor

The background color of the RequiredFieldValidator control

ControlToValidate

The id of the control to validate

Error Message

The text to display in the page when validation fails.

ForeColor

The foreground color of the control

id

A unique id for the control

Initial Value

Specifies the starting value of the input control. Default value is ""

IsValid

A Boolean value that indicates whether the control specified by ControlToValidate is determined to be valid

runat

Specifies that the control is a server control. Must be set to "server"

 

Code:

  
<asp:Label ID="Label2" runat="server" Text="Name"></asp:Label>
  <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
 
    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox1"
        ErrorMessage="Enter your Name"></asp:RequiredFieldValidator>
 
    <asp:Label ID="Label3" runat="server" Text="ID"></asp:Label>
    <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
 
    <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="TextBox2"
        ErrorMessage="Enter your ID"></asp:RequiredFieldValidator>
 
    <asp:Button ID="Button1" runat="server" Text="Save" OnClick="Button1_Click" />
    <asp:Label ID="Label1" runat="server"></asp:Label>
 
 
protected void Button1_Click(object sender, EventArgs e)
    {
      
            Label1.Text = " form is valid!";
     
 
    }
 

 

When we will not enter any value in the TextBox and when we will click save button then error message will be display.

 

RequiredFieldValidator Control in ASP.Net

When we enter value in TextBox then page will execute

RequiredFieldValidator Control in ASP.Net

 


Updated 07-Sep-2019

Leave Comment

Comments

Liked By