如果多个文本框不相等,如何比较它们并触发验证器

本文关键字:比较 验证 文本 不相等 何比较 如果 | 更新日期: 2023-09-27 18:07:31

问题是我需要一种方法来比较用户输入的4个文本框与用户输入的另外6个文本框的值。如果它们不匹配,那么启动验证以停止进程。我使用一个名为"lblH1"的标签来显示它们需要匹配文本框。是否有可能使用比较验证器或ranger验证器控件来做到这一点?

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 %>')