[Javascript] Adding values in input boxes

Mckinney, Lori K lori.mckinney at intergraph.com
Thu Jun 1 09:01:30 CDT 2006


How about using the isNaN(testValue) function for your test?  NaN is not
a string.
 
total = isNaN(x1)?0:x1 + isNaN(y1)?0:y1 + isNaN(z1)?0:z1;
 
http://www.devguru.com/Technologies/ecmascript/quickref/isnan.html

________________________________

From: javascript-bounces at LaTech.edu
[mailto:javascript-bounces at LaTech.edu] On Behalf Of Alan Easton
Sent: Thursday, June 01, 2006 8:42 AM
To: javascript at LaTech.edu
Subject: [Javascript] Adding values in input boxes


Hello People,
 
I have 4 input boxes, 3 so I can enter numeric values in(c1, c2, c3),
and the 4th to display the total.(total1)
 
I have put onblur actions on each of the 3 boxes like such:
onBlur="addNumbers();"
 
My function is:
---------------------------------------------
function addNumbers() { 
  
    var x1, y1, z1
 var x = parseInt(frmAIMSsignup.c1.value);
 var y = parseInt(frmAIMSsignup.c2.value);
 var z = parseInt(frmAIMSsignup.c3.value);
 
 if (x != 'NaN')
 {
 x1 = x;
 }
 else
 {
 x1 = 0;
 }
 if (y != 'NaN')
 {
 y1 = y;
 }
 else
 {
 y1 = 0;
 }
 if (z != 'NaN')
 {
 z1 = z;
 }
 else
 {
 z1 = 0;
 }
    var totcol1 = x1 + y1 + z1;

  
    frmAIMSsignup.total1.value = totcol1; 
} 
 
</script>
 
---------------------------------------------
 
I am adding then OK, except when no numeric value is in the input box. I
simply want to be able to add up the 3 boxes when any of them change,
but also catch when nothing has been entered.i.e. when the value is
undefined.
 
Does anyone have any ideas. I would appreciate any help.
 
Many Thanks,
 
Al...
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.evolt.org/pipermail/javascript/attachments/20060601/b726a79d/attachment.htm>


More information about the Javascript mailing list