Click here to Skip to main content
15,904,156 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
I am using below function, for calculation. Only issue, is during when using input in 1st txtbox, result displays as NaN, I want to display, either Zero, or input value from 1st txtbox.

function show(TxtSTUPort, TxtAAPort, STD_NUM_OF_SERVING) 
{
    var TxtSTUPort = document.getElementById(TxtSTUPort);
    var TxtAAPort = document.getElementById(TxtAAPort);
    var STD_NUM_OF_SERVING = document.getElementById(STD_NUM_OF_SERVING);
    STD_NUM_OF_SERVING.value = (Number(parseInt(TxtSTUPort.value) + parseInt(TxtAAPort.value)));
}
Posted
Updated 8-Mar-11 4:40am
v2

The problem is that you're trying to use them as if there's a valid numeric value in them. Before trying to use the values in those fields, check to see if they're NaN, and only then, do the desired math operations. In fact, I'd do this:

var TxtSTUPort = document.getElementById(TxtSTUPort);    
var TxtAAPort = document.getElementById(TxtAAPort);    
var STD_NUM_OF_SERVING = document.getElementById(STD_NUM_OF_SERVING);
var stuPort = 0;
var aaPort = 0;
var numServing = 0;

if (TxtSTUPort != null && TxtSTUPort.value != null)
{
    stuPort = TxtSTUPort.value;
}
if (TxtAAPort != null && TxtAAPort.value != null)
{
    aaPort = TxtAAPort.value;
}
if (STD_NUM_OF_SERVING != null)
{
    STD_NUM_OF_SERVING.value = stuPort + aaPort;
}


Id also take steps to ensure that the data IN the text fields is valid numeric data. I'm not big on JavaScript, but I'm sure there's code somewhere to do that (and the code above may need to be tweeked since I'm not sure if comparing against null is valid in Javascript).
 
Share this answer
 
v3
Comments
Rohit.Net100 8-Mar-11 11:12am    
I get my soultion. Thanks for help. This is what i changed.

function show(TxtSTUPort, TxtAAPort, STD_NUM_OF_SERVING) {
var TxtSTUPort = document.getElementById(TxtSTUPort);
var TxtAAPort = document.getElementById(TxtAAPort);
var STD_NUM_OF_SERVING = document.getElementById(STD_NUM_OF_SERVING);
STD_NUM_OF_SERVING.value = parseInt(TxtSTUPort.value) + parseInt(TxtAAPort.value);
if (!parseInt(STD_NUM_OF_SERVING.value)) {
STD_NUM_OF_SERVING.value = '';
}
}
fjdiewornncalwe 8-Mar-11 16:50pm    
+5. Why. Because you're right.
Monjurul Habib 8-Mar-11 17:22pm    
nice one , my 5+
change function like this-
C#
function show(TxtSTUPort, TxtAAPort, STD_NUM_OF_SERVING)
{
    var TxtSTUPort = document.getElementById(TxtSTUPort);
    var TxtAAPort = document.getElementById(TxtAAPort);
    var STD_NUM_OF_SERVING = document.getElementById(STD_NUM_OF_SERVING);
    var stuPort=parseInt(TxtSTUPort.value);
    var aaPort=parseInt(TxtAAPortTxtSTUPort.value);
    stuPort=isNaN(stuPort)?0:stuPort;
    aaPort=isNaN(aaPort)?0:aaPort;
    STD_NUM_OF_SERVING.value = (Number(stuPort + aaPort));
}



--Pankaj
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900