File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "adding numbers " Watch "adding numbers " New topic

adding numbers

sweta naidu

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.

Eric Pascarello

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

KasiMurugan Ramasamy
Ranch Hand

Joined: Jan 30, 2005
Posts: 125

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

Joined: Nov 02, 2004
Posts: 25
Thanks. it solved my problem.
I agree. Here's the link:
subject: adding numbers
It's not a secret anymore!