Support us .Net Basics C# SQL ASP.NET ADO.NET MVC Slides C# Programs Subscribe Buy DVD

RequiredField validator control in - Part 44

Suggested Videos
Part 41 - Literal control in
Part 42 - panel control
Part 43 - Creating controls dynamically using panel control

Validation controls are used to ensure if, the data, entered by the user is valid. Microsoft framework, provides 6 built-in validation controls. 
1. RequiredFieldValidator
2. RangeValidator 
3. RegularExpressionValidator
4. CompareValidator
5. CustomValidator    
6. ValidationSummary

RequiredField validator control in

These validation controls can be used to perform both client side and server side validation.

Browser's understand only client scripts and HTML. In the past to perform client side validation, developers had to write themselves the required javascript code. With validation controls, we don't have to write javascript, we can use the built-in validation controls, which will generate the required javascript for us.

Client scripts can spread viruses and cause security concerns. Because of this, users may disable JavaScript on their browser. If this happens, client side validation is skipped. That is why, it is always a good practice to have server side validation as well.

The validation control's also perform server side validation. Server side validation is always performed, irrespective of whether the client side validation is done or not.

In this video we will discuss about RequiredField validator control. This control, ensures that the required field is entered by the user. Let us understand with an example.

RequiredField validator control in

Consider the HTML below. TextBox with ID="txtName" expects the user to enter their name. This is required field. Next to this textbox is a RequiredFieldValidator control, which is used to ensure that the user has entered his name. ControlToValidate property specifies the control to validate. ErrorMessage is the message that is displayed when the validation fails. To validate ddlGender DropDownList, we have RequiredFieldValidatorGender. We have specified InitialValue="-1". This will ensure that, if the user has selected, "Select Gender" option from the DropDownList, the control will still fail the validation.
            :<asp:TextBox ID="txtName" runat="server" Width="150px">
            <asp:RequiredFieldValidator ID="RequiredFieldValidatorName" runat="server" 
            ErrorMessage="Name is required" ForeColor="Red"
            ControlToValidate="txtName" Display="Dynamic" >
            :<asp:DropDownList ID="ddlGender" runat="server" Width="150px">
                <asp:ListItem Text="Select Gender" Value="-1"></asp:ListItem>
                <asp:ListItem Text="Male" Value="Male"></asp:ListItem>
                <asp:ListItem Text="Female" Value="Female"></asp:ListItem>
            <asp:RequiredFieldValidator ID="RequiredFieldValidatorGender" runat="server" 
            ErrorMessage="Gender is required" ForeColor="Red" InitialValue="-1"
            ControlToValidate="ddlGender" Display="Dynamic" >
        <td colspan="2">
            <asp:Button ID="btnSave" runat="server" Text="Save" Width="100px" 
        <td colspan="2">
            <asp:Label ID="lblStatus" runat="server" Font-Bold="true">

Code-Behind code: Page.IsValid is a read-only property. This property returns true if all the validation controls has passed validation. Use this property to perform server side validation. 
protected void btnSave_Click(object sender, EventArgs e)
    if (Page.IsValid)
        lblStatus.ForeColor = System.Drawing.Color.Green;
        lblStatus.Text = "Data saved successfully!";
        lblStatus.ForeColor = System.Drawing.Color.Red;
        lblStatus.Text = "Data not valid and not saved!";

Run the project, and notice that the client side validation is performed. Now, disable Javascript. To disable javascript in, internet explorer
1. Click on Tools on the Menu Bar. If the Menu bar is not visible, press ALT+T.
2. From the Tools menu, select Internet Options
3. Click on the Security tab, on the internet options window
4. Select Local Intranet icon and click on Cutom Level button
5. Scroll down in the Security Settings window, and find Scripting option in the list.
6. Click Disable radiobutton under active scripting
7. Click OK and click Yes on the warning.

The above steps should disable javascript. Run the project now, and click Save button, without entering any data. Notice, that the client side validation is skipped, but the server side validation is performed.


  1. Hi Venkat
    I am Getting this Error when i use validaton controls in webform.
    WebForms UnobtrusiveValidationMode requires a ScriptResourceMapping for 'jquery'. Please add a ScriptResourceMapping named jquery(case-sensitive).
    Can u solve this

  2. Register jQuery in Global.asax in the Application_Start:

    new ScriptResourceDefinition
    Path = "~/scripts/jquery-1.7.2.min.js",
    DebugPath = "~/scripts/jquery-1.7.2.min.js",
    CdnPath = "",
    CdnDebugPath = ""

  3. This exception occurs because ASP.NET is expecting you to define how validation should be rendered. You
    can enable unobtrusive validation by adding the following to the web.configfile:

    The value of ValidationSettings:Unobtrusive Validation Mode can be None or WebForms.
    ➤ WebForms means that you would like to use the Web Forms unobtrusive validation mode.
    ➤ None means that you do not want to use unobtrusive validation
    If you don’t already have jQuery installed in Visual Studio, here’s how you do it. Using the
    Package Manager in Visual Studio 2012, you’ll install two different packages. To access
    Package Manager, go to View ➪Other Windows ➪Package Manager Console.
    1.The first package we need to install is jQuery. This allows ASP.NET to use the jQuery
    JavaScript framework. When the package is installed, the scripts are placed in a scripts
    folder. To install jQuery using the Package Manager, enter
    install-package jQuery. When executing this, the latest version of jQuery is installed.
    2. The second package we need to install will add jQuery to the ScriptManager object
    in the PreApplicationStartupMethod. To install this package using the Package
    Manager, enter install-package AspNet.ScriptManager.jQuery

  4. For those who get UnobtrusiveValidationMode error. Just go to web.config file and add this snippet of code under tag:


If you like this website, please share with your friends on facebook and Google+ and recommend us on google using the g+1 button on the top right hand corner.