如何在验证后使用Page_ClientValidate成功执行自定义代码

时间:2011-08-12 04:51:26

标签: asp.net validation

我有两种类型的提交,一种是必需的,另一种是不需要的。

如果用户按下提交按钮,那么我调用一个功能,我有设置逻辑用于检查

  • 1。必填字段验证(如果用户将在所需文件中输入所有详细信息)
  • 2。检查不是必填字段(然后我必须提示用户您没有输入以下详细信息...是否要填写这些详细信息?是|否)
  • 3。如果我按是,则页面不应提交
  • 4。其他页面应提交
  • 我做了所有的事情,但它不起作用,以下是我的代码片段

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

    2 个答案:

    答案 0 :(得分:1)

    “for”循环中存在语法错误。将循环的开始更改为以下内容:

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

    通过此更改以及您具有功能逻辑的方式,用户将收到确认提示,如果用户单击“取消”,则页面将不会回发。

    答案 1 :(得分:0)

    您可以在提交表单时调用js方法的修订版本,该版本应在验证触发后发生。如果用户想要回答不需要的值,请修改您的js以取消提交。