Reputation: 969
The issue I have is that I need a way to compare the values from 4 textboxes input by user with another 6 textboxes input by user. If they dont match then fire validation to stop progress. I use a label called 'lblH1' to show they need to match textboxes. Is it possible to use compare validator or ranger validator controls to do this?
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";
}
Upvotes: 2
Views: 1765
Reputation: 19591
You can try CustomValidator
for this, like shown below
<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>
Things to be noted:
I have used ClientIDMode="Static"
just for the sake of making code compact, please ignore it if you can and use document.getElementById('<%= txtH2A_6.ClientID %>')
etc.
Upvotes: 1