• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

adding numbers

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

Eric
 
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks. it solved my problem.
 
reply
    Bookmark Topic Watch Topic
  • New Topic