Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / ASP.NET

Validate multiple Validation group both client and server side in ASP.NET

4.80/5 (4 votes)
11 Jun 2012CPOL 43.6K  
There is a simple solution for multiple validation group check in a page.

Introduction

I found a big problem in ASP.NET when I used multiple validation groups in a page. The Button click event does not check every validation group.

Using the code

I assumed that you have controls with multiple validation group, like this: 

HTML
<asp:DropDownList ID="ddlIndBrand" runat="server" CssClass="contentControlWidth2"
        DataTextField="BrandName" DataValueField="BrandID" ValidationGroup="DBudgetEntry"
        Width="150px">
    </asp:DropDownList>
    <asp:RequiredFieldValidator ID="RequiredFieldValidator15" 
        runat="server" ControlToValidate="ddlIndBrand"
        Display="None" ErrorMessage="Please select a Brand!" 
        ForeColor="Red" InitialValue="0"
        SetFocusOnError="True" Text="?" 
        ValidationGroup="group1"></asp:RequiredFieldValidator>
<asp:TextBox ID="txt" runat="server" Columns="5" 
      CssClass="contentControlWidth2"
      Height="70px" TextMode="MultiLine" 
      ValidationGroup="DBudgetEntry" Width="215px"></asp:TextBox>
        <asp:RequiredFieldValidator ID="RequiredFieldValidator16" 
          runat="server" ControlToValidate="txt"
          Display="None" ErrorMessage="Details required!" 
          ForeColor="Red" SetFocusOnError="True"
          Text="?" ValidationGroup="group2"></asp:RequiredFieldValidator>
    <asp:TextBox ID="txt2" runat="server" 
        Columns="5" CssClass="contentControlWidth2"
        Height="70px" TextMode="MultiLine" 
        ValidationGroup="DBudgetEntry" Width="215px"></asp:TextBox>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator16" 
               runat="server" ControlToValidate="txt2"
               Display="None" ErrorMessage="Details required!" 
               ForeColor="Red" SetFocusOnError="True"
               Text="?" ValidationGroup="group3"></asp:RequiredFieldValidator> 
<asp:Button ID="btnSave" runat="server" 
  Text="Save" Visible="True" Width="60px" 
  OnClientClick="javascript:return validatePage();"/>

Write this on the top of the .aspx page:

JavaScript
<script language="javascript" type="text/javascript">
    function validatePage() {
        //Executes all the validation controls associated with group1 validaiton Group1. 
        var flag = window.Page_ClientValidate('group1');
        if (flag)
      //Executes all the validation controls associated with group1 validaiton Group2. 
           flag = window.Page_ClientValidate('group2');
        if (flag)
      //Executes all the validation controls associated with group1 validaiton Group3. 
          flag = window.Page_ClientValidate('group3');
        if (flag)
        //Executes all the validation controls which are not associated with any validation group. 
            flag = window.Page_ClientValidate();
        return flag;
    } 
</script>

This validates the client side. But for the server side, write this on the btnsave click event:

C#
protected void btnSave_Click(object sender, EventArgs e)
{
    Page.Validate();
    if (!Page.IsValid)
    {
        return;
    }
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)