开发者

How to execute cutom code after validation get success with Page_ClientValidate

I have two types of filed one is Required and Other is Not Required.

If user will press submit button then i call a function where i have setup logic for checking

  • 1. Required Field Validation (if user would enter all the details in required file then)
  • 2. Check for Not Required Field (then i have to prompt user for You have not entered following details ... Do you want to fill these details? Yes | No)
  • 3. If i press Yes then page should not submit
  • 4. else page should submit
  • i have done all the thing but it is not working, following are my code snippet

    <script language="javascript" type="text/javascript">
            function CheckEmptyField() {
                var isPageValid;
                if (typeof (Page_ClientValidate) == 'function') {
                    isPageValid = Page_ClientValidate();
                    if (isPageValid) {
                        var elementNotRequired = document.getElementsByTagName("span")
                        var strvalidationMessage = "<h5>You haven’t entered the following columns:</h5><br/><ul>";
                        for (var ele = 0; elementNotRequired.length; ele++) {
                            if (elementNotRequired[ele].className == 'NotRequired') {
                                strvalidationMessage += "<li>" + elementNotRequired[ele].innerText + "</li>";
                            }
                        }
                        strvalidationMessage += "</ul><br/> <h6>Do you want to enter these details?</h6>";
                        isPageValid = confirm(strvalidationMessage);
                    }
                }
                return isPageValid;
            }
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <table cellpadding="1" cellspacing="1" width="650px">
                <tr>
                    <td colspan="2">
                        Following fields are <b>Required!</b>
                    </td>
                    <td colspan="2">
                        Following fields are <b>Not Required!</b>
                    </td>
                </tr>
                <tr>
                    <td>
                        First Name
                    </td>
                    <td>
                        <asp:TextBox runat="server" ID="txtFirstName"></asp:TextBox>
                        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtFirstName"
                            ErrorMessage="First Name is required" ForeColor="Red">*</asp:RequiredFieldValidator>
                    </td>
                    <td>
                        Education
                    </td>
                    <td>
                        <asp:TextBox runat="server" ID="txtEducation"></asp:TextBox>
                        <span class="NotRequired">Education</span>
                    </td>
                </tr>
                <tr>
                    <td>
                        Last Name
                    </td>
                    <td>
                        <asp:TextBox runat="server" ID="txtLastName"></asp:TextBox>
                        <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ControlToValidate="txtLastName"
                            ErrorMessage="Last Name is required" ForeColor="Red">*</asp:RequiredFieldValidator>
                    </td>
                    <td>
                        Company Name
                    </td>
                    <td>
                        <asp:TextBox runat="server" ID="txtCompanyName"></asp:TextBox>
                        <span class="NotRequired">Company Name</span>
                    </td>
                </tr>
                <tr>
                    <td>
                  开发者_Python百科      Age
                    </td>
                    <td>
                        <asp:TextBox runat="server" ID="txtAge"></asp:TextBox>
                        <asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" ControlToValidate="txtAge"
                            ErrorMessage="Age is required" ForeColor="Red">*</asp:RequiredFieldValidator>
                    </td>
                    <td>
                    </td>
                    <td>
                    </td>
                </tr>
                <tr>
                    <td>
                    </td>
                    <td>
                        <asp:Button runat="server" ID="btnSubmit" Text="Submit" OnClientClick="return CheckEmptyField();" />
                        <asp:ValidationSummary ID="ValidationSummary1" runat="server" ForeColor="Red" />
                    </td>
                    <td colspan="2">
                    </td>
                </tr>
            </table>
        </div>
    


    You have a syntax error in your "for" loop. Change the start of your loop to the following:

    for ( var ele = 0; ele < elementNotRequired.length; ele++ )
    

    With this change and the way you have your function logic, the user will get the confirm prompt and if the user clicks "Cancel", the page will not post back.


    You could call a revised version of your js method on submit of the form, which should occur after the validations have fired. Revise your js to cancel the submission if the user wants to answer the non-required values.

    0

    上一篇:

    下一篇:

    精彩评论

    暂无评论...
    验证码 换一张
    取 消

    最新问答

    问答排行榜