aspose file tools*
The moose likes HTML, CSS and JavaScript and the fly likes adding numbers Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "adding numbers " Watch "adding numbers " New topic
Author

adding numbers

sweta naidu
Greenhorn

Joined: Nov 02, 2004
Posts: 25
i am facing a problem while populating calculated value in the text field.
here is my js function
function Total()
{
document.forms[0].p_phase.value = parseInt(document.forms[0].p_scope.value) +
parseInt(document.forms[0].p_plan.value )+ parseInt(document.forms[0].p_design.value)
+ parseInt(document.forms[0].p_build.value )+ parseInt(document.forms[0].p_anal.value)
+ parseInt(document.forms[0].p_systest.value )+ parseInt(document.forms[0].p_uat.value)

}

this code works if values entered for all the fileds if not it displays NaN. but i want this to function even if some values entered.

Thanks,
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
you need to check for isNaN before adding or set a default value of 0 to the fields.

Eric
KasiMurugan Ramasamy
Ranch Hand

Joined: Jan 30, 2005
Posts: 125
Hi

For each value, if follow the below like logic you could get the desired results.

var currentVal = document.forms[0].p_scope.value;
if( currentVal.length >0 && !isNaN( currentVal) )
total = total + parseInt( currentVal );


Thanks & Regards
Kasimurugan (SCJP1.4, SCBCD1.3), Preparing SCWCD1.4
sweta naidu
Greenhorn

Joined: Nov 02, 2004
Posts: 25
Thanks. it solved my problem.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: adding numbers