Two Validation Groups in a single button without client click event

asp.netcustomvalidatorvalidation

In a multigrid I am validating two controls like date and amount. It is validating correctly when I press the tabevent. When I press the save button it is not validating. I am using two validations groups and two validation summary. Then in save button Ialso tried onclientclick() function with javascript it is working fine but if I give the correct value in date and amount records, it is not saving. how to over come this.

Date

 <asp:TemplateField HeaderText="Date">
                                                        <ItemTemplate>
                                                            <asp:TextBox ID="txtDate" Text='<%# Bind("AD_REF_DATE") %>' runat="server" CausesValidation="true"
                                                                ValidationGroup="group" Width="80px" AutoPostBack="true" OnTextChanged="txtDate_TextChanged"></asp:TextBox>
                                                        </ItemTemplate>
                                                    </asp:TemplateField>
                                                    <asp:TemplateField>
                                                        <ItemTemplate>
                                                            <asp:CompareValidator ID="dateValidater" runat="server" ControlToValidate="txtDate"
                                                                Operator="DataTypeCheck" Type="Date" ValidationGroup="group" EnableClientScript="true"
                                                                ErrorMessage="Please enter a valid date (mm/dd/yyyy)." SetFocusOnError="true" Display="None">*</asp:CompareValidator>
                                                        </ItemTemplate>
                                                    </asp:TemplateField>

Amount

<asp:TemplateField HeaderText="Amount">
                                                        <ItemTemplate>
                                                            <asp:TextBox ID="txtAmount" MaxLength="17" Text='<%# Bind("AD_AMOUNT") %>' CausesValidation="true"
                                                                ValidationGroup="req" runat="server" AutoPostBack="true">
                                                            </asp:TextBox>
                                                        </ItemTemplate>
                                                    </asp:TemplateField>
                                                    <asp:TemplateField>
                                                        <ItemTemplate>
                                                            <asp:RegularExpressionValidator ID="regVal1" runat="server" ControlToValidate="txtAmount"
                                                                ErrorMessage="Format(13int,5deci)" ValidationExpression="^[1-9]\d{0,12}(\.\d{1,2})?%?$"
                                                                ValidationGroup="req" Display="None"  EnableClientScript="true" SetFocusOnError="true">
                                                            </asp:RegularExpressionValidator>
                                                        </ItemTemplate>
                                                    </asp:TemplateField>

validations summary:

 <asp:ValidationSummary ID="ValidationSummary3" runat="server" ValidationGroup="req"
                            HeaderText="Amount:Invalid Format" DisplayMode="BulletList" ShowMessageBox="true"
                            ShowSummary="false" />
                        <asp:ValidationSummary ID="ValidationSummary1" runat="server" ValidationGroup="group"
                            HeaderText="Date:Invalid Format" DisplayMode="BulletList"  ShowMessageBox="true" 
                            ShowSummary="false" />

button save:

 <asp:Button ID="ButtonSave" runat="server"  CssClass="button" CausesValidation="true" Text="<%$Resources:TJFAS501, ButtonSave %>"
                                            OnClick="ButtonSave_Click" TabIndex="6" />

How to validate this in the button save also two popup box should be shown?

Best Answer

You might want to try firing the validate function for both groups manually via the button's OnClientClick since you have two validation groups you need to validate against. Currently your validation is not firing because you do not have any ValidationGroup assigned to your button so it is just looking for validators with no ValidationGroup defined (yours groups are: group and req).

You can call Page_ClientValidate() via javascript to fire off the validation checks manually (be sure to set CauseValidation on your button to false) and it has an optional parameter that takes the validation group.

<asp:Button ID="yourButton" runat="server" OnClick="ButtonSave_Click"
    CausesValidation="false" TabIndex="6"
    OnClientClick="return (Page_ClientValidate('group') && Page_ClientValidate('req'));" />

You can read more about Page_ClientValidate on MSDN.

It would be easier to just have one validation group for each action (eg. your button) but I assume you need two groups for some reason.