Link to home
Start Free TrialLog in
Avatar of dontlookbehind
dontlookbehind

asked on

summing numbers

Hi, I get the values from some form fields..which is the expression to mathematically sum them?
Tnx
Avatar of zvonko
zvonko

sum = (document.forms[0].field1.value*1)+(document.forms[0].field2.value*1);

This was without content checking for valid number content, but essential it works this way.

Basically are all values untyped in JavaScript. Therefore you can simply do multiplication of string content by one to get a number. Otherwise it would concatenate two string values.

Do you need more details?
Avatar of fritz_the_blank
Here is an example--you can add as many text fields as you like, just make sure that they start their name with the first three letters "arg":

<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
<TITLE></TITLE>
<SCRIPT LANGUAGE=javascript>
<!--
function sumArguments(){
     var numSum;
     numSum=0;
     for(i=0;i<document.forms[0].elements.length;i++){
          //alert(document.forms[0].elements[i].name.substring(0,3))
          if(document.forms[0].elements[i].name.substring(0,3)=="arg"){
               if(isNaN(document.forms[0].elements[i].value)){
                    alert("Please enter a valid number in each of the fields!");
                    document.forms[0].elements[i].focus();
                    return false;
               }
               numSum = numSum + parseFloat(document.forms[0].elements[i].value,10);
          }
     }
     document.forms[0].numTotal.value = numSum;
     return true;
}
//-->
</SCRIPT>

</HEAD>
<BODY><FORM action="" method=POST id=form1 name=form1>
First<INPUT type="text" id=text1 name=argFirst><br>
Second<INPUT type="text" id=text2 name=argSecond><br>
Third<INPUT type="text" id=text2 name=argThird><br>
Sum<INPUT type="text" id=text3 name=numTotal onFocus="javascript:blur()"><br>
<INPUT type="button" value="Add Me!" id=button1 name=button1 onClick="javascript:sumArguments()">
</FORM>
</BODY>
</HTML>

Fritz the Blank
ASKER CERTIFIED SOLUTION
Avatar of zvonko
zvonko

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
try this.
assume its text field.
function fnSum(){
//assign form object to a variable
frm=document.forms[0]
var nTotal=0;
nTotal=parseInt(frm.textfield1.value)+parseInt(frm.textfield2.value)
}

// if for multiple text field with same text field name try this
function fnSum(){
//assign form to a variable
frm=document.forms[0]
var nTotal=0;
for(i=0;i<frm.textfield.length;i++){
nTotal+=parseInt(frm.textfield[i].value)
}
alert(nTotal)
}
shankar_m75,

Your solution will not work if the person wants to sum non-interger values, like currency for example. Also, if someone enters a non-numeric value, your code will throw an ungraceful error at the user.

Fritz the Blank
dontlookbehind,

Why the grade of B? You accepted ZVonko's answer and it does exactly what you want it to. Shouldn't s/he get an A?

Fritz the Blank
dontlookbehind :-)