Click here to Skip to main content
15,881,719 members
Articles / Web Development / ASP.NET
Tip/Trick

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

Rate me:
Please Sign up or sign in to vote.
4.80/5 (4 votes)
11 Jun 2012CPOL 43K   3   2
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)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionNice Article Pin
Siva Hyderabad18-Feb-14 20:07
Siva Hyderabad18-Feb-14 20:07 
GeneralMy vote of 5 Pin
taha bahraminezhad Jooneghani12-Jun-12 6:16
taha bahraminezhad Jooneghani12-Jun-12 6:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.