Click here to Skip to main content
Click here to Skip to main content

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

, 11 Jun 2012
Rate this:
Please Sign up or sign in to vote.
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: 

<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:

<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:

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)

About the Author

cseppd

United States United States
No Biography provided

Comments and Discussions

 
QuestionNice Article PinmemberSambasivaRaoG18-Feb-14 20:07 
GeneralMy vote of 5 Pinmembertaha bahrami12-Jun-12 6:16 

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

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

| Advertise | Privacy | Mobile
Web01 | 2.8.140721.1 | Last Updated 11 Jun 2012
Article Copyright 2012 by cseppd
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid