Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
57
rated 0 times [  60] [ 3]  / answers: 1 / hits: 18550  / 11 Years ago, mon, april 22, 2013, 12:00:00

All this is a little bit over my experience but I have looked everywhere I could, and researched this stuff for 6 hours so far today and am just running into brick walls.



I have a table, where a user enters two variables and from those two variables 12 different numbers are spit out.
The problem those numbers are spit out as plain text strings into readonly text fields, and I need them to be displayed as (US) currency.
All of the input fields in the table are using onchange to run the first function which does the calculations, but the second and third functions which I found online I can not get to run.



HTML:



 <form name=form >

<table width=550 border=0>
<tr>
<td width=265 height=30><div align=right>Number of Business Customers</div></td>
</tr>
<TR>
<td width=265 height=30><div align=right>Number of Business Clients:</div></td>
<td width=142 div align=center><input style=font-size:12px;text-align:center; name=sum1 onChange=updatesum();CurrencyFormatted();CommaFormatted90; /></td>
<td width=129 div align=center>&nbsp;</td>
</tr>
<TR>
<td height=30><div align=right>Average Number of Employees:</td>
<TD div align=center><input style=font-size:12px;text-align:center; name=sum2 onChange=updatesum();CurrencyFormatted();CommaFormatted(); /></TD>
<TD div align=center>&nbsp;</TD>
<TR>
<td height=30><div align=right>Anticipated Employee Tax Reduction:</div></td>
<TD div align=center><input style=font-size:12px;text-align:center;border:none; name=sum16 readonly ></TD>
<TD div align=center><input style=font-size:12px;text-align:center;border:none name=sum26 readonly><BR /></TD>
</TR>
<TR>
<td height=30><div align=right>Potential Payroll Tax Reduction (annually):</div></td>
<TD div align=center><input style=font-size:12px;text-align:center;border:none name=sum17 readonly ></TD>
<TD div align=center><input style=font-size:12px;text-align:center;border:none name=sum27 readonly ></TD>
</TR>
<TR>
<td height=30><div align=right>Potential Payroll Tax Reduction (monthly):</td>
<TD div align=center><input type=text style=font-size:12px;text-align:center;border:none name=sum18 readonly ></td>
<TD div align=center><input style=font-size:12px;text-align:center;border:none name=sum28 readonly ></td>
</TR>
<TR>
<td height=30><div align=right>Pearl Logic Billing (50% of savings, for 12 months):</td>
<TD div align=center><input style=font-size:12px;text-align:center;border:none name=sum19 readonly ></td>
<TD div align=center><input style=font-size:12px;text-align:center;border:none name=sum29 readonly ></td>
</TR>
<TR>
<td height=30><div align=right>Sales Agent Monthly Comp (8%):</td>
<TD div align=center><input style=font-size:12px;text-align:center;border:none name=sum14 readonly ></td>
<TD div align=center><input style=font-size:12px;text-align:center;border:none name=sum24 readonly ></td>
</TR>
<TR>
<td height=30><div align=right>Sales Agent Total Comp (8%):</td>
<TD div align=center><input style=font-size:12px;text-align:center;border:none name=sum15 readonly ></td>
<TD div align=center><input style=font-size:12px;text-align:center;border:none name=sum25 readonly ></td>
</TR>
</table>
</form>


Javascript:



function updatesum() {




document.form.sum14.value = ((((((document.form.sum1.value -0)*(document.form.sum2.value -0)*300))/12)/2)*.08);
document.form.sum24.value = ((((((document.form.sum1.value -0)*(document.form.sum2.value -0)*400))/12)/2)*.08);
document.form.sum15.value = (((((((document.form.sum1.value -0)*(document.form.sum2.value -0)*300))/12)/2)*.08)*12);
document.form.sum25.value = (((((((document.form.sum1.value -0)*(document.form.sum2.value -0)*400))/12)/2)*.08)*12);
document.form.sum16.value = 300;
document.form.sum26.value = 400;
document.form.sum17.value = ((document.form.sum1.value -0)*(document.form.sum2.value -0)*300);
document.form.sum27.value = ((document.form.sum1.value -0)*(document.form.sum2.value -0)*400);
document.form.sum18.value = (((document.form.sum1.value -0)*(document.form.sum2.value -0)*300)/12);
document.form.sum28.value = (((document.form.sum1.value -0)*(document.form.sum2.value -0)*400)/12);
document.form.sum19.value = ((((document.form.sum1.value -0)*(document.form.sum2.value -0)*300)/12)/2);
document.form.sum29.value = ((((document.form.sum1.value -0)*(document.form.sum2.value -0)*400)/12)/2);
}


//--></script>

<script type=text/javascript><!--
function CurrencyFormatted()
{
var i = parseFloat(amount);
if(isNaN(i)) { i = 0.00; }
var minus = '';
if(i < 0) { minus = '-'; }
i = Math.abs(i);
i = parseInt((i + .005) * 100);
i = i / 100;
s = new String(i);
if(s.indexOf('.') < 0) { s += '.00'; }
if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
s = minus + s;
return s;
}
//--></script>
<script type=text/javascript><!--
function CommaFormatted()
{
var delimiter = ,; // replace comma if desired
var a = amount.split('.',2)
var d = a[1];
var i = parseInt(a[0]);
if(isNaN(i)) { return ''; }
var minus = '';
if(i < 0) { minus = '-'; }
i = Math.abs(i);
var n = new String(i);
var a = [];
while(n.length > 3)
{
var nn = n.substr(n.length-3);
a.unshift(nn);
n = n.substr(0,n.length-3);
}
if(n.length > 0) { a.unshift(n); }
n = a.join(delimiter);
if(d.length < 1) { amount = n; }
else { amount = n + '.' + d; }
amount = minus + amount;
return amount;
}

//--></script>

More From » html

 Answers
1

Your functions have an undefined amount variable. Presumably they just crash with an error.



First you need to remove those function calls from the onChange listener.



Second, change the functions to take an unformatted value and return a formatted one



function CurrencyFormatted(amount) { //<-- notice the method parameter
var i = parseFloat(amount);
if(isNaN(i)) { i = 0.00; }
var minus = '';
if(i < 0) { minus = '-'; }
i = Math.abs(i);
i = parseInt((i + .005) * 100);
i = i / 100;
s = new String(i);
if(s.indexOf('.') < 0) { s += '.00'; }
if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
s = minus + s;
return s;
}


Now change each of your set calls in your updatesum to first format the value



var sum14 = ((((((document.form.sum1.value -0)*(document.form.sum2.value -0)*300))/12)/2)*.08);
var formattedSum14 = CommaFormatted(CurrencyFormatted(sum14));
document.form.sum14.value = sum14;

[#78709] Monday, April 22, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
anyssaarielles

Total Points: 415
Total Questions: 107
Total Answers: 92

Location: Greenland
Member since Fri, Jul 31, 2020
4 Years ago
;