Required Field Validation Controls in Asp.Net C#


The RequiredFieldValidator control ensures that the required field is not empty. It is generally tied to a text box to force input into the text box.

RequiredFieldValidator validation control make an web server input control as like a mandatory field.


Html Coding 

<html xmlns="">
<head id="Head1" runat="server">
    <form id="form1" runat="server">
        <asp:Label ID="Label1" runat="server" Text="Name"></asp:Label>
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator1"
                ControlToValidate="TextBox1" ErrorMessage="*"></asp:RequiredFieldValidator>
        <asp:Label ID="Label2" runat="server" Text="Country"></asp:Label>
            <asp:DropDownList ID="DropDownList1" runat="server">
                <asp:ListItem>Select Country</asp:ListItem>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator2"
ErrorMessage="Please Select Country"
                InitialValue="Select Country"></asp:RequiredFieldValidator>
    <tr><td colspan="2">
        <asp:Button ID="Button1" runat="server" Text="Save" />


You can use it to make sure that the user has entered something in a TextBox control

BackColorThe background color of the RequiredFieldValidator control
ControlToValidateThe id of the control to validate
DisplayThe display behavior for the validation control. Legal values are:
  • None (the control is not displayed. Used to show the error message only in the ValidationSummary control)
  • Static (the control displays an error message if validation fails. Space is reserved on the page for the message even if the input passes validation.
  • Dynamic (the control displays an error message if validation fails. Space is not reserved on the page for the message if the input passes validation
EnableClientScriptA Boolean value that specifies whether client-side validation is enabled or not
EnabledA Boolean value that specifies whether the validation control is enabled or not
ErrorMessageThe text to display in the ValidationSummary control when validation fails.Note: This text will also be displayed in the validation control if the Text property is not set
ForeColorThe foreground color of the control
idA unique id for the control
InitialValueSpecifies the starting value of the input control. Default value is ""
IsValidA Boolean value that indicates whether the control specified by ControlToValidate is determined to be valid
runatSpecifies that the control is a server control. Must be set to "server"
TextThe message to display when validation fails

First  - Add - New WebForm - Select  Textbox & DropDownlist  from ToolBox  

Next - Go to - ToolBox -  Validation -Select - Required Field Validation - for  Textbox & DropDownlist

 Next - Select Required Field Validation - F4 press Property window  below like that

Property Select - Error Message Change Required  - Select  Control to Validate TextBox id Values for Control Textbox 

Select - DropDownList -Required Field Validation - Error Message write required - Control to validate DropDownList ID - Initial values Put Select Country  or your dropdownlist values -  index 0 

Next -  Press F5 - Run the Program 

Next  - Click Save Button  Without Fill  Values  Show below like that  Error Message Show 

Next -  Fill Values After Click - Save Button  - Save the values 


Post a Comment