我有一个带有 5-6 个带有验证的输入字段的 web 表单,它工作正常,现在我需要为 T&C 添加 checkboc,除非选中 T&C 复选框,否则表单不应提交。
下面是我的部分代码,为了简单起见,我删除了一些输入以保持简短。
我正在使用OnClientClick="ClientSideClick(this)"
功能来检查验证以禁用多次提交的提交按钮。
此表单有效并且如果我不检查复选框验证但是当我尝试验证并且如果“Page_ClientValidate”为真时,它永远不会检查复选框验证的代码部分。我尝试将代码放在不同的位置,但它不起作用。不是我做错了什么
<asp:Label ID="lblFirstName" runat="server" CssClass="row-label" Text="First Name:"></asp:Label>
<asp:TextBox ID="txtFirstName" runat="server" CssClass="row-input"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfFN" runat="server" ValidationGroup="VG" ErrorMessage="*" ControlToValidate="txtFirstName" Display="Dynamic"></asp:RequiredFieldValidator>
<asp:Label ID="Label2" runat="server" CssClass="row-label" Text="Last Name:"></asp:Label>
<asp:TextBox ID="txtLastName" runat="server" CssClass="row-input"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvtxtLastName" runat="server" ValidationGroup="VG" ErrorMessage="*" ControlToValidate="txtLastName" Display="Dynamic"></asp:RequiredFieldValidator>
<asp:Label ID="lblEmail" runat="server" CssClass="row-label" Text="E-mail:"></asp:Label>
<asp:TextBox ID="txtEmail" runat="server" CssClass="row-input"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvEmail" runat="server" ValidationGroup="VG" ErrorMessage="*" ControlToValidate="txtEmail" Display="Dynamic"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="revEmail" runat="server" ErrorMessage="*" ControlToValidate="txtEmail" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" ValidationGroup="VG" Display="Dynamic"></asp:RegularExpressionValidator>
<asp:CheckBox ID="chkIAgreeSub" runat="server" /> I agree to Terms & condition
<asp:Button ID="btnSave" runat="server" CssClass="btn btn-submit" Text="Submit" OnClick="btnSave_Click" OnClientClick="ClientSideClick(this)" UseSubmitBehavior="False" ValidationGroup="VG" />
<script type="text/javascript">
//Avoid Multiple Submission
function ClientSideClick(myButton) {
// Client side validation
if (typeof (Page_ClientValidate) == 'function') {
if (Page_ClientValidate() == false)
{
console.log("VALIDATION FALSE");
return false;
}
else
{
console.log("VALIDATION TRUE");
if (document.getElementById("<%=chkboxTC.ClientID %>").checked == true) {
console.log("T&C checked TRUE");
return true;
} else {
console.log("T&C NOT checked FALSE");
return false;
}
}
}
if (myButton.getAttribute('type') == 'button') {
// diable the button
myButton.disabled = true;
myButton.value = "Processing...";
}
return true;
}
</script>