RequiredFieldValidator Control in ASP.NET

RequiredFieldValidator Control

As we all know we need to do client-side scripting to validate input or to check whether all mandatory fields have been filled in by the user. So for doing such kind of validation we can either write our own JavaScript code or we can make use of built-in validation controls to validate the code. ASP.Net provides several validation controls (such as RequiredFieldValidator, CompareValidator, RegularExpressionValidator, RangeValidator etc.). Of all of these we will have a look at RequiredFieldValidator.

RequiredFieldValidator is basically doing validations against the required fields in the front end. The user must fill in all the mandatory fields, to ensure this we make use of RequiredFieldValidator.

It has certain properties that we need to set for it to function the way we want it to, for example: it should show an error message whenever the details are not filled in. For a login form we want the user name and word, both fields, to be filled in; only then can the user click the button and the server-side processing can occur. If either of the fields are empty then an error message should be invoked or displayed.

This task is quiet simple, because in case of a login form we just want to perform validation against the two required fields (UserName and word). In the default case, whenever there is a RequiredFieldValidator inside a page and whenever the button is clicked then it will validate all the controls falling inside that page, but sometimes there exists a necessity for two or more panels and each one should be independently validated when the respective button within that panel is clicked. To do that we have to register the RequiredFieldValidator against the group of controls falling inside that particular panel.

Let's say we have a login form and the following with a corresponding forgotten word table.


Whenever the Log In button is clicked then only the user name and word fields should be validated and whenever the Getword button is clicked then only the UserName field should be validated which is inside the forgotten word panel. For doing such things we can make use of a ValidationGroup property of RequiredFieldValidator.

The following is the source code for that:

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>  
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">  
  3. <html xmlns="">  
  4. <head runat="server">  
  5.     <title></title>  
  6. </head>  
  7. <body>  
  8.     <form id="form1" runat="server">  
  9.     <div>  
  10.          <p id="panel1">  
  11.             <table>  
  12.                 <tr>  
  13.                     <td>User Name:</td>  
  14.                     <td>  
  15.                         <asp:TextBox ID="txtName" ValidationGroup="LoginFrame" runat="server"></asp:TextBox>  
  16.                     </td>  
  17.                     <td>  
  18.                         <asp:RequiredFieldValidator ID="reqName" ControlToValidate="txtName" ValidationGroup="LoginFrame" runat="server" ErrorMessage="*"></asp:RequiredFieldValidator>  
  19.                     </td>  
  20.                 </tr>  
  21.                 <tr>  
  22.                     <td>word:</td>  
  23.                     <td>  
  24.                         <asp:TextBox ID="txt" runat="server" ValidationGroup="LoginFrame"></asp:TextBox>  
  25.                     </td>  
  26.                     <td>  
  27.                         <asp:RequiredFieldValidator ID="reqword" ControlToValidate="txt" ValidationGroup="LoginFrame" runat="server" ErrorMessage="*"></asp:RequiredFieldValidator>  
  28.                      </td>  
  29.                 </tr>  
  30.                 <tr>  
  31.                     <td colspan="2" align="right">  
  32.                         <asp:Button ID="btnSubmit" runat="server" Text="Log In" ValidationGroup="LoginFrame" />  
  33.                     </td>  
  34.                 </tr>  
  35.             </table>  
  36.          </p>  
  37.          <p>  
  38.              Incase you forgot your word...  
  39.          </p>  
  40.          <p id="panel2">  
  41.                 <table>  
  42.                     <tr>  
  43.                          <td>User Name:</td>  
  44.                         <td>  
  45.                             <asp:TextBox ID="txtUserName" ValidationGroup="ForgotwordFrame" runat="server"></asp:TextBox>  
  46.                         </td>  
  47.                         <td>  
  48.                             <asp:RequiredFieldValidator ID="reqUserName" ControlToValidate="txtUserName" ValidationGroup="ForgotwordFrame" runat="server" ErrorMessage="*"></asp:RequiredFieldValidator>  
  49.                         </td>  
  50.                     </tr>  
  51.                     <tr>  
  52.                         <td>  
  53.                             <asp:Button ID="btnGetword" runat="server" Text="Get word" ValidationGroup="ForgotwordFrame" />  
  54.                         </td>  
  55.                     </tr>  
  56.                 </table>  
  57.          </p>  
  58.     </div>  
  59.     </form>  
  60. </body>  
  61. </html> 

So in the above example we have created two validationgroups; one for panel1 and the second for panel2 with the name as LoginFrame and ForgotwordFrame. The controls inside panel1 all have the ValidationGroup property set to LoginFrame and for the 2nd Panel all controls have the ValidationGroup Property set to ForgotwordFrame. In this way we can reduce the overhead of writing complex JavaScript code and can make use of builtin Validation Controls.

If you click the Log In button the following is the output. See only the login Frame controls are being validated.


If you click the Getword button then only the controls inside the forgotten word frame are being validated.


Hope you all have liked the article and it may help you in your programming.