2014-06-09 34 views
2

我遇到的問題是,我需要一種方法來比較用戶輸入的4個文本框與用戶輸入的另外6個文本框的值。如果他們不匹配,那麼火驗證停止進展。我使用名爲'lblH1'的標籤來顯示他們需要匹配文本框。是否有可能使用比較驗證器或遊俠驗證器控件來做到這一點?如何比較多個文本框和消防驗證器,如果他們不等於c#

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"; 
} 
+0

你想驗證它的客戶端或服務器端? 你想使用JavaScript來做到嗎? –

+0

你想驗證文本框中的值或這些值的總和嗎?在代碼中,你正在比較總和。 – yogi

+0

對不起服務器端,如果我可以 - 真的不知道enugh Javascript – wubblyjuggly

回答

1

您可以嘗試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 %>')