Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys,
I have been working on this problem over a quarter day and tried many things but succeed.

I have a custom validator that has an OnServerValidate method and it belongs to a validation group along with other required field validators.
My submit button calls a JavaScript function to check if the validations on the page are complete and if valid, expand the next Accordion Pane(this can be done using client side script).

The problem i am facing is that, the following Javascript method returns

Page_IsValid to true even though the Server Validation method returns, Args.IsValid=False.

How is that possible that a validation inside a validation group is ignored?

Here is the code.
txtCity and txtPostalCode validations needs to be done here.
Postal code validation needs to be done on the server via database query.

XML
<asp:TextBox ID="txtCity" runat="server" Height="19px" Width="250px" MaxLength="25"></asp:TextBox>
                                     <asp:RequiredFieldValidator ID="ReqtxtSenderCity" runat="server"  validationgroup="PersonalInfoGroup"
                                    ControlToValidate="txtCity" Display="Dynamic" ErrorMessage="*"
                                     ></asp:RequiredFieldValidator>


XML
<asp:TextBox ID="txtPostalCode" runat="server" Height="18px" Width="250px"></asp:TextBox>


                           <asp:CustomValidator id="CustomValidator1" runat="server"
                           ControlToValidate="txtPostalCode"
                           ValidateEmptyText="true"
                           OnServerValidate="ZipStateValidate"  validationgroup="PersonalInfoGroup"
                           ErrorMessage="*Zip Code is invalid" dispay="dynamic">
                           </asp:CustomValidator>


Here is the server Method


protected void ZipStateValidate(object source, System.Web.UI.WebControls.ServerValidateEventArgs args)
{

    if (ddlState.SelectedValue.ToString().Length > 0)
    {

        int intProvinceCode;
        string strZip = string.Empty;
        intProvinceCode = Convert.ToInt16(ddlState.SelectedValue);
        strZip = args.Value.ToString();
        BusinessLayer obj = new BusinessLayer();
        DataSet IsValidZip = obj.IsValidZIP(intProvinceCode, strZip);
        int isValid = 0;
        foreach (DataRow dr1 in IsValidZip.Tables[0].Rows)
        {

            isValid = Convert.ToInt16(dr1[0].ToString());

        }


        if (isValid == 1)
        {
            args.IsValid = true;
        }
        else
        {
            args.IsValid = false;
        }

    }
}

Here is the JavaScript Method

C#
// expand given accordion pane
                           function changeSelected(idx) {



                               if (Page_ClientValidate('PersonalInfoGroup')) {
                                  var ExtenderName = "<% =Accordion1.ClientID %>" + "_AccordionExtender"
                               var accordion = $find(ExtenderName);
                               accordion.set_SelectedIndex(idx);

                               }
                               else {
                                   return Page_IsValid;
                               }


                           }



Here is the button that initiates the validation.
(Window.setTimeout was suggested at one website, that's why I am using it but it does not have any effect on the execution)

<asp:Button id="btnValidate" Autopostback="true" OnClick ="btnValidate_click" CausesValidation="true" OnClientClick= "window.setTimeout(function(){ changeSelected(1); },0);"  validationgroup="PersonalInfoGroup" runat="server" Text="Next"></asp:Button>



As a result of the button click,
1) When txtCity has a value and when txtPostalCode has an invalid value, the Page_ClientValidate('PersonalInfoGroup') method returns true and the Accordion Pane expands,
2) After the accordion pane expands, the ServerSide validation gets completed and I see the validation errors from my custom validator. The validation works but does not affect the Page_ClientValidate.

My question is, Is it possible to run "all" the validations in the same validation group before the java script method executes. In this case, custom validator seems to be executed late.

If not, do you have any suggestions?
Thanks
Posted
Updated 14-Oct-13 7:17am
v3

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900