Link to home
Start Free TrialLog in
Avatar of PNKJ
PNKJ

asked on

Auto calculate and add $ sign in javascript

Hi,

I have a form where I have six text box one each for the money spent on travel, food, other expenses and a total for these three fields. Another set is for education, advertisement and  a total for these two fields. My data taype is float and I want to display a $ sign when the user enters in any text box.
The total for the first set  travel, food and other expenses should be calculated automatically whenever the values in any of the text box change. The toal for the second set should also be calculated  Then I have GRand total field which is sum of the total of both sets and should be calculated and displayed with $ sign
Avatar of hielo
hielo
Flag of Wallis and Futuna image

It will be better if you post your form OR provide a link so we see the form.
And if it is homework, please do some of it yourself
Avatar of PNKJ
PNKJ

ASKER

Hi,

Please see the code below I have in my form.
code.txt
1. I would use onKeyUp and pass the form:

onKeyUp="calc(this.form)"

then in calc I would calculate ALL values

I wrote a script for you, but my editor crashed. I will see if I can get a temp file back
Avatar of PNKJ

ASKER

Thanks  but my problem is that i need to display $ sign in each text box when the value is entered and also in both total field and grand total and whwever th euser changes the value in any input textbox the total field and grand total should reflect the changes with $ sign in front. I had tried this function to add dollar sign but for som reason when I use this function it only displays the dollar field in totals of each section but Grand total shows Nan and individal text box does not show $ sign.
<!-- Begin
function formatCurrency(num) {
num = num.toString().replace(/\$|\,/g,'');
if(isNaN(num))
num = "0";
sign = (num == (num = Math.abs(num)));
num = Math.floor(num*100+0.50000000001);
cents = num%100;
num = Math.floor(num/100).toString();
if(cents<10)
cents = "0" + cents;
for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
num = num.substring(0,num.length-(4*i+3))+','+
num.substring(num.length-(4*i+3));
return (((sign)?'':'-') + '$' + num + '.' + cents);
}
//  End -->
</script>
</HEAD>

<!-- STEP TWO: Copy this code into the BODY of your HTML document  -->

<BODY>
ASKER CERTIFIED SOLUTION
Avatar of hielo
hielo
Flag of Wallis and Futuna image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of PNKJ

ASKER

Hi I tried to use this function in may asp.net page but I get error object expected. Is it possible to call this function on keyup event or keydown
   myMasterpage.SetBodyOnLoadScript("init();")
 Public Sub SetBodyOnLoadScript(ByVal strOnLoad As String)
        body.Attributes.Add("onload", "javascript:" & strOnLoad)
    End Sub
>>I tried to use this function
What function?

Your post makes no sense. I am not familiar with your environment. What I gave you works as you desired. I tested it as a stand alone page. You just need to make your server generate the HTML code I gave you (minus the commented out server code of course. I left those there just so you know which server fields correspond with which <input type="text"> tags).
Avatar of PNKJ

ASKER

Thanks it works absolutely fine but I am calling it on onblur. It didn't work on onload event for some reason.