Calculating form field values
-
Hi I have a form set up currently with a number of fields which the user must submit numeric data to. Which then gets stored into a database which I now have all working fine.:) 2 of the fields will include totals of data which they will enter: The total amount of people The total amount problems with people (a person could have more than one problem) Then there are 3 different categories of questions which someone must fill in. For one category it has 3 fields to fill in, what i would like is that the 3 values added must match the total amount added of problems with people. Another category has 5 fields to fill in which should match the total number of people filled in earlier in the form. If someone tries to submit the form with an incorrect match of people to the totals it wont allow them to sumbit the form. Could anyone give me some pointers as to how best to do this? I use C# Thanks
-
Hi I have a form set up currently with a number of fields which the user must submit numeric data to. Which then gets stored into a database which I now have all working fine.:) 2 of the fields will include totals of data which they will enter: The total amount of people The total amount problems with people (a person could have more than one problem) Then there are 3 different categories of questions which someone must fill in. For one category it has 3 fields to fill in, what i would like is that the 3 values added must match the total amount added of problems with people. Another category has 5 fields to fill in which should match the total number of people filled in earlier in the form. If someone tries to submit the form with an incorrect match of people to the totals it wont allow them to sumbit the form. Could anyone give me some pointers as to how best to do this? I use C# Thanks
Hi there. Just a suggestion - do you need them to enter the totals at all? How about if you just have them enter the 3 fields (or 5 fields), then you compute the total on the server side? If you want to show the total as they are entering values, you could add a little client-side javascript that adds up the fields as they enter values dynamically (perhaps using
onkeydown
oronchange
events client-side). -
Hi there. Just a suggestion - do you need them to enter the totals at all? How about if you just have them enter the 3 fields (or 5 fields), then you compute the total on the server side? If you want to show the total as they are entering values, you could add a little client-side javascript that adds up the fields as they enter values dynamically (perhaps using
onkeydown
oronchange
events client-side).Hi Thanks for the reply The totals do need to be added as they want to be able to view totals quickly from a results page we have created which totals all figures from certain categories. Could you point me in the direction of any javascripts that I can download to do this?;) Thanks
-
Hi Thanks for the reply The totals do need to be added as they want to be able to view totals quickly from a results page we have created which totals all figures from certain categories. Could you point me in the direction of any javascripts that I can download to do this?;) Thanks
Here's a simple example. This is a straight .htm page (doesn't need to be an .aspx) showing the simple javascript applied to each text box.
<html>
<head>
<script type="text/javascript">
function ComputeTotal()
{
var t1 = document.getElementById("text1");
var t2 = document.getElementById("text2");
var t3 = document.getElementById("text3");// if any of the boxes contain characters that are not numeric, // treat it as a zero var v1 = (isNaN(parseInt(t1.value)) ? 0 : parseInt(t1.value)); var v2 = (isNaN(parseInt(t2.value)) ? 0 : parseInt(t2.value)); var v3 = (isNaN(parseInt(t3.value)) ? 0 : parseInt(t3.value)); return (v1 + v2 + v3); } function OutputTotal() { var vTotal = ComputeTotal(); var tTotal = document.getElementById("spanTotal"); tTotal.innerHTML = vTotal; } </script>
</head>
<body>
<h3>Simple JavaScript to compute a total</h3>
This one uses the <code>onkeyup</code> event of the text boxes to compute the totals.
<br />
Enter three values:
<br/>
<input type="text" id="text1" onkeyup="OutputTotal();"/><br />
<input type="text" id="text2" onkeyup="OutputTotal();"/><br />
<input type="text" id="text3" onkeyup="OutputTotal();"/><br />
Total: <span id="spanTotal" style="font-weight: bold;"></span>
<br />
</body></html>
-
Here's a simple example. This is a straight .htm page (doesn't need to be an .aspx) showing the simple javascript applied to each text box.
<html>
<head>
<script type="text/javascript">
function ComputeTotal()
{
var t1 = document.getElementById("text1");
var t2 = document.getElementById("text2");
var t3 = document.getElementById("text3");// if any of the boxes contain characters that are not numeric, // treat it as a zero var v1 = (isNaN(parseInt(t1.value)) ? 0 : parseInt(t1.value)); var v2 = (isNaN(parseInt(t2.value)) ? 0 : parseInt(t2.value)); var v3 = (isNaN(parseInt(t3.value)) ? 0 : parseInt(t3.value)); return (v1 + v2 + v3); } function OutputTotal() { var vTotal = ComputeTotal(); var tTotal = document.getElementById("spanTotal"); tTotal.innerHTML = vTotal; } </script>
</head>
<body>
<h3>Simple JavaScript to compute a total</h3>
This one uses the <code>onkeyup</code> event of the text boxes to compute the totals.
<br />
Enter three values:
<br/>
<input type="text" id="text1" onkeyup="OutputTotal();"/><br />
<input type="text" id="text2" onkeyup="OutputTotal();"/><br />
<input type="text" id="text3" onkeyup="OutputTotal();"/><br />
Total: <span id="spanTotal" style="font-weight: bold;"></span>
<br />
</body></html>
Thanks for this, that works great, however:rolleyes: what im looking to do is put the results in a hidden text field which I then put a compare validator on in visual studio. Ill have a go at fiddling with this to do it, but any tips will be appreciated!