gpt4 book ai didi

c# - 如何比较多个文本框并在它们不相等时触发验证器 C#

转载 作者:太空宇宙 更新时间:2023-11-03 13:21:10 27 4
gpt4 key购买 nike

我遇到的问题是我需要一种方法来比较用户输入的 4 个文本框的值与用户输入的另外 6 个文本框的值。如果它们不匹配,则触发验证以停止进程。我使用名为“lblH1”的标签来显示它们需要匹配文本框。是否可以使用比较验证器或游侠验证器控件来执行此操作?

h1_total = Convert.ToInt32(txtKS_1.Text) + Convert.ToInt32(txtKS_2.Text) + 
Convert.ToInt32(txtKS_3.Text) + Convert.ToInt32(txtKS_4.Text);

H2A_total = Convert.ToInt32(txtH2A_1.Text) + Convert.ToInt32(txtH2A_2.Text) +
Convert.ToInt32(txtH2A_3.Text) + Convert.ToInt32(txtH2A_4.Text) +
Convert.ToInt32(txtH2A_5.Text) + Convert.ToInt32(txtH2A_6.Text);

if (h1_total == H2A_total)
{
//save
}
else
{
lblH1.Visible = true;
lblH1.Text= "Values must match";
}

最佳答案

你可以试试CustomValidator为此,如下所示

<head runat="server">
<title></title>
<script>
function validate(sender, arg) {
debugger;
var h1_total =
(document.getElementById("txtKS_1").value == "" ? 0 : parseFloat(document.getElementById("txtKS_1").value)) +
(document.getElementById("txtKS_2").value == "" ? 0 : parseFloat(document.getElementById("txtKS_2").value)) +
(document.getElementById("txtKS_3").value == "" ? 0 : parseFloat(document.getElementById("txtKS_3").value)) +
(document.getElementById("txtKS_4").value == "" ? 0 : parseFloat(document.getElementById("txtKS_4").value));

var H2A_total =
(document.getElementById("txtH2A_1").value == "" ? 0 : parseFloat(document.getElementById("txtH2A_1").value)) +
(document.getElementById("txtH2A_2").value == "" ? 0 : parseFloat(document.getElementById("txtH2A_2").value)) +
(document.getElementById("txtH2A_3").value == "" ? 0 : parseFloat(document.getElementById("txtH2A_3").value)) +
(document.getElementById("txtH2A_4").value == "" ? 0 : parseFloat(document.getElementById("txtH2A_4").value)) +
(document.getElementById("txtH2A_5").value == "" ? 0 : parseFloat(document.getElementById("txtH2A_5").value)) +
(document.getElementById("txtH2A_6").value == "" ? 0 : parseFloat(document.getElementById("txtH2A_6").value));

if (h1_total != H2A_total)
arg.IsValid = false;
else
arg.IsValid = true;
}

</script>
</head>
<body>
<form id="form1" runat="server">
<div>

<asp:TextBox runat="server" ID="txtKS_1" ClientIDMode="Static" />
<asp:TextBox runat="server" ID="txtKS_2" ClientIDMode="Static" />
<asp:TextBox runat="server" ID="txtKS_3" ClientIDMode="Static" />
<asp:TextBox runat="server" ID="txtKS_4" ClientIDMode="Static" />
<br />
<asp:TextBox runat="server" ID="txtH2A_1" ClientIDMode="Static" />
<asp:TextBox runat="server" ID="txtH2A_2" ClientIDMode="Static" />
<asp:TextBox runat="server" ID="txtH2A_3" ClientIDMode="Static" />
<asp:TextBox runat="server" ID="txtH2A_4" ClientIDMode="Static" />
<asp:TextBox runat="server" ID="txtH2A_5" ClientIDMode="Static" />
<asp:TextBox runat="server" ID="txtH2A_6" ClientIDMode="Static" />
<br />
<asp:CustomValidator runat="server" ID="vali1" ErrorMessage="Values must be same." ClientValidationFunction="validate" ValidationGroup="validate" />
<asp:Button runat="server" ID="btn" Text="Click me" ValidationGroup="validate" />
</div>
</form>
</body>

注意事项:

我用过ClientIDMode="Static"只是为了代码简洁,可以的话请忽略,使用document.getElementById('<%= txtH2A_6.ClientID %>')等等

关于c# - 如何比较多个文本框并在它们不相等时触发验证器 C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24116216/

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