Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

adding numbers

 
sweta naidu
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you need to check for isNaN before adding or set a default value of 0 to the fields.

Eric
 
KasiMurugan Ramasamy
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 );
 
sweta naidu
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. it solved my problem.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic