javascript - addition

They have: 1 posts

Joined: Feb 2005

Hi, I am new to this site and javascript.

I am trying to get the below function to work, however total_rate returns NaN instead of a number. I am not sure what I did wrong any help would be appreciated. Thanks.

if (document.frm_emplyee_data.cbo_PayCycle.value == "M1" || document.frm_emplyee_data.cbo_PayCycle.value == "W2")

{
var num_pay_rate = parseFloat(document.frm_emplyee_data.txt_payrate.value);
var num_acct_rate1 = parseFloat(document.frm_emplyee_data.txt_acct_dist_rate1.value);
var num_acct_rate2 = parseFloat(document.frm_emplyee_data.txt_acct_dist_rate2.value);
var num_acct_rate3 = parseFloat(document.frm_emplyee_data.txt_acct_dist_rate3.value);

var total_rate = (num_acct_rate1 + num_acct_rate2 + num_acct_rate3);

if (total_rate != num_pay_rate)
{
alert("Pay Rate must be equal to sum of all rate fields in Account Distribution" + total_rate );
document.frm_emplyee_data.txt_payrate.focus();
flag=0;
return false
}
}

CptAwesome's picture

He has: 370 posts

Joined: Dec 2004

NaN means Not a Number. So it is reading whatever you are trying to add as a string. The solution I have seen is declaring the variable like this:

var number = 0+0;

So the the number starts as zero, but as a number, not as a string.

They have: 12 posts

Joined: Dec 2004

Are you talking about server side JS?

Renegade's picture

He has: 3,022 posts

Joined: Oct 2002

JS is client side. Not server side. Server side is something like PHP, ASP and Coldfusion

CptAwesome's picture

He has: 370 posts

Joined: Dec 2004

Well, isn't JSP techincally javascript and server side?

They have: 12 posts

Joined: Dec 2004

Quote: Well, isn't JSP techincally javascript and server side?

thats what i ment.
The code here isn't server side obviously but my point was that this board is called Server-Side Scripting and this thread might be better off somewhere else.

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.