gpt4 book ai didi

javascript - 带有复选框验证的 Asp.net Web 表单无法正常工作

转载 作者:行者123 更新时间:2023-11-28 17:29:10 25 4
gpt4 key购买 nike

我有一个带有 5-6 个输入字段的网络表单,并进行了验证,工作正常,现在我需要为 T&C 添加 checkboc,除非选中 T&C 复选框,否则表单不应提交。

为了简单起见,我在下面的部分代码中删除了一些输入以保持简洁。

我正在使用 OnClientClick="ClientSideClick(this)" 函数来检查验证以禁用多次提交的提交按钮。

如果我不检查复选框验证,则此表单有效,但是当我尝试验证并且“Page_ClientValidate”为 true 时,它​​永远不会检查复选框验证的代码部分。我尝试将代码放在不同的地方,但它不起作用。不是我做错了什么

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

最佳答案

您的代码不起作用,因为 asp:CheckBox 呈现为

<label id="the_long_webforms_id">
<input type="checkbox"/>
I agree to Terms & condition
</label>

所以这条线

document.getElementById("<%=chkboxTC.ClientID %>").checked

失败。你将需要类似的东西

// provided a class from the checkbox from preventing using the client id
// then took the child that is input:checkbox of the label
e.IsValid = document.querySelector('.agree-terms input').checked;
<小时/>

更简单的方法是使用这个

<asp:CheckBox ID="chkIAgreeSub" runat="server"
CssClass="agree-terms"
Text="I agree to Terms & condition">
</asp:CheckBox>
<asp:CustomValidator ID="CheckBoxRequired" runat="server"
EnableClientScript="true"
ErrorMessage="You must select this box to proceed"
ValidationGroup="VG"
ClientValidationFunction="CheckBoxRequired_ClientValidate">
</asp:CustomValidator>

JavaScript

function CheckBoxRequired_ClientValidate(sender, e)
{
e.IsValid = document.querySelector('.agree-terms input').checked;
//e.IsValid = jQuery(".agree-terms input:checkbox").is(':checked');
}

关于javascript - 带有复选框验证的 Asp.net Web 表单无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50850750/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com