• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

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.
 
Thanks tiny ad, for helping me escape the terrible comfort of this chair.
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic