Click to See Complete Forum and Search --> : Adding running totals...NaN error.
Rashar
02-06-2004, 07:58 AM
Hi, I have the following script that will add numbers together. What I want to happen though is if a user enters 2 numbers to add together, to populate the answer in a textbox called Total_Hours. If they want to add 3 numbers together, add the total into the same box. I have it set up so that you can add a total of 5 numbers. My Problem is if a user enters 3 numbers or 2 numbers to add (not all 5), then when I hit calculate, I get the NaN error message. Should I be using If statements in this case?
Thanks in advance,
Rashar
<script language="javascript">
//adding total hours
function CalcHours()
{
var hours1 = parseInt(document.FrontPage_Form1.hr1.value);
var hours2 = parseInt(document.FrontPage_Form1.hr2.value);
var hours3 = parseInt(document.FrontPage_Form1.hr3.value);
var hours4 = parseInt(document.FrontPage_Form1.hr4.value);
var hours5 = parseInt(document.FrontPage_Form1.hr5.value);
document.FrontPage_Form1.Total_Hours.value = hours1 + hours2;
document.FrontPage_Form1.Total_Hours.value = hours1 + hours2 + hours3;
document.FrontPage_Form1.Total_Hours.value = hours1 + hours2 + hours3 + hours4;
document.FrontPage_Form1.Total_Hours.value = hours1 + hours2 + hours3 +hours4 + hours5;
}
</script>
Pittimann
02-06-2004, 08:19 AM
Hi!
You can do it like this for example:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled</title>
<script language="JavaScript" type="text/javascript">
<!--
var hours1;
var hours2;
var hours3;
var hours4;
var hours5;
function calcHours() {
hours1=0;
hours2=0;
hours3=0;
hours4=0;
hours5=0;
if (document.FrontPage_Form1.hr1.value!=""&&!isNaN(document.FrontPage_Form1.hr1.value)){
hours1 = parseInt(document.FrontPage_Form1.hr1.value);
}
if (document.FrontPage_Form1.hr2.value!=""&&!isNaN(document.FrontPage_Form1.hr2.value)){
hours2 = parseInt(document.FrontPage_Form1.hr2.value);
}
if (document.FrontPage_Form1.hr3.value!=""&&!isNaN(document.FrontPage_Form1.hr3.value)){
hours3 = parseInt(document.FrontPage_Form1.hr3.value);
}
if (document.FrontPage_Form1.hr4.value!=""&&!isNaN(document.FrontPage_Form1.hr4.value)){
hours4 = parseInt(document.FrontPage_Form1.hr4.value);
}
if (document.FrontPage_Form1.hr5.value!=""&&!isNaN(document.FrontPage_Form1.hr5.value)){
hours5 = parseInt(document.FrontPage_Form1.hr5.value);
}
document.FrontPage_Form1.Total_Hours.value = hours1 + hours2 + hours3 +hours4 + hours5;
}
//-->
</script>
</head>
<body>
<form name="FrontPage_Form1">
<input type =text name="hr1"><br>
<input type =text name="hr2"><br>
<input type =text name="hr3"><br>
<input type =text name="hr4"><br>
<input type =text name="hr5"><br>
<input type =text name="Total_Hours"><br>
<input type =button onClick="calcHours()" value="Calculate"><br>
</body>
</html>
Cheers - Pit
Rashar
02-06-2004, 09:06 AM
That's exactly what I was looking for... I didn't know how to pass a calculation from an input box that did not have a number... I could have set an initial value to 0, but this way is much better.
Thanks again,
Rashar.
Pittimann
02-06-2004, 09:15 AM
Hi!
You are welcome!
I could have shortened the code considerably by looping through the fields, but due to the fact that I don't know about how deep you already dived into js, I did it this way :rolleyes:...
Cheers - Pit
Rashar
02-06-2004, 10:27 AM
Hi, I only need it for 5 fields, so this should be good enough. But thanks for your help on this one. It's working perfect.
Rashar
Pittimann
02-06-2004, 10:44 AM
Hi!
Anyway, why not shorten it :p?
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled</title>
<script language="JavaScript" type="text/javascript">
<!--
var hours1;
var hours2;
var hours3;
var hours4;
var hours5;
function calcHours() {
for (var i =1; i <=5; i++){
var temp=eval('document.FrontPage_Form1.hr'+i+'.value');
if (temp!=""&&!isNaN(temp)){
eval('hours'+i+' = parseInt(temp)');
}
else{
eval('hours'+i+' = 0');
}
}
document.FrontPage_Form1.Total_Hours.value = hours1 + hours2 + hours3 +hours4 + hours5;
}
//-->
</script>
</head>
<body>
<form name="FrontPage_Form1">
<input type =text name="hr1"><br>
<input type =text name="hr2"><br>
<input type =text name="hr3"><br>
<input type =text name="hr4"><br>
<input type =text name="hr5"><br>
<input type =text name="Total_Hours"><br>
<input type =button onClick="calcHours()" value="Calculate"><br>
</body>
</html>
Cheers - Pit
Rashar
02-06-2004, 12:32 PM
This is great. It all makes sense now.
Thanks
Rashar
Pittimann
02-06-2004, 12:38 PM
Hi!
I just added that for fun ;)
You're welcome again!!
Cheers - Pit
VIAwebguy
03-28-2005, 03:43 PM
I need to have 3 column of info--the first will contain the name of a car part.
The second will contain the price.
The third will contain a check box per row for the user to check the parts he wants to total at the bottom.
*I need to have about 5 empty boxes at the bottom of each of the three columns for the user to be able to add up to 5 additional parts names and their prices. Check boxes aren't needed for these last 5 but if info (a price) is entered it needs to be added into the total of everything at the bottom.
I have 173 parts, so I need at least 178 rows. I just spent several hours in sever failed attempts. Is there a piece of shareware or a Frontpage add-in that will allow me to create this EASILY (or at all!!) that is menu-driven?
HELP!!! Thanks!