• Status: Solved
• Priority: Medium
• Security: Public
• Views: 1444

# Auto Sum form Fields with Currency values

A friend of mine wrote me a javascript function that checks the current input and adds a dollar sign and appropriate decimal point.

I am really stretching my JS knowledge here, but I am sure the dollar sign is causing conflict with some auto-sum code i am using the form as well.

``````<script type="text/javascript">
// Matt Roberts 6/1/2009 - Added this script to format the amount fields with dollar sign, decimal point, and comma separators.
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));
if (num =="0") {
return(" ");
} else {
return (((sign)?'':'-') + '\$' + num + '.' + cents);
}
}
</script>

<script type="text/javascript">
document.form.finTotal.value = (document.form.finIncTot.value -0) + (document.form.finIncWEF.value -0) + (document.form.finAMS.value -0) + (document.form.finPENS.value -0) + (document.form.finEDU.value -0) + (document.form.finDMF.value -0);
}

</script>
<fieldset><legend>Financial Summary</legend>
<table border="0">
<tr>
<td><p>Total Church Income</p>      </td>
<td><input name="finIncTot" type="text" id="finIncTot" onChange="updatesum()" onBlur="this.value=formatCurrency(this.value)"/>      </td>
</tr>
<tr>
<td><p>Given to WEF Fund</p>      </td>
<td><input name="finIncWEF" type="text" id="finIncWEF" onChange="updatesum()" onBlur="this.value=formatCurrency(this.value)"/>      </td>
</tr>
<tr>
<td><p>Given to Approved Missions Specials</p>      </td>
<td><input name="finAMS" type="text" onChange="updatesum()" onBlur="this.value=formatCurrency(this.value)" />      </td>
</tr>
<tr>
<td><p>Given to Pension Fund</p>      </td>
<td><input name="finPENS" type="text" onChange="updatesum()" onBlur="this.value=formatCurrency(this.value)" />      </td>
</tr>
<tr>
<td><p>Given to Education Fund</p>      </td>
<td><input name="finEDU" type="text" onChange="updatesum()" onBlur="this.value=formatCurrency(this.value)" />      </td>
</tr>
<tr>
<td><p>Given to District Mission Fund</p>      </td>
<td><input name="finDMF" type="text" onChange="updatesum()" onBlur="this.value=formatCurrency(this.value)" />      </td>
</tr>
<tr>
<td><p><strong>Total Given to Global &amp; District Missions</strong></p>      </td>
</tr>
</table>
``````
0
axessJosh
• 4
• 4
1 Solution

Author Commented:
Actually, i removed the = '\$' and it works. I figured the dollar sign was messing up the math.

now, everything works fine except for my total field.  It doesn't auto add the decimal until all the fields are done in an 'onFocus' event.

what can i put to have that always updating with the decimals?
0

Systems architectCommented:
Here how I would do:
``````<html>
<title>Zvonko &#42;</title>
<script type="text/javascript">
var theForm = theField.form;
var fldName = "finIncTot|finIncWEF|finAMS|finPENS|finEDU|finDMF".split('|');
var theTotal = 0;
for(var i=0;i<fldName.length;i++){
var theField = theForm[fldName[i]];
var theValue = theField.value.replace(/[^\d\.\-]/g,"");
if(theValue>""){
theTotal = theTotal + (theValue*1);
theField.value = "\$"+(theValue*1).toFixed(2);
} else {
theField.value = "";
}
}
theForm.finTotal.value = "\$"+theTotal.toFixed(2);
}
</script>
<body>
<form>
<fieldset><legend>Financial Summary</legend>
<table border="0">
<tr>
<td><p>Total Church Income</p>      </td>
<td><input name="finIncTot" type="text" id="finIncTot" onChange="updatesum(this)" />      </td>
</tr>
<tr>
<td><p>Given to WEF Fund</p>      </td>
<td><input name="finIncWEF" type="text" id="finIncWEF" onChange="updatesum(this)" />      </td>
</tr>
<tr>
<td><p>Given to Approved Missions Specials</p>      </td>
<td><input name="finAMS" type="text" onChange="updatesum(this)" />      </td>
</tr>
<tr>
<td><p>Given to Pension Fund</p>      </td>
<td><input name="finPENS" type="text" onChange="updatesum(this)" />      </td>
</tr>
<tr>
<td><p>Given to Education Fund</p>      </td>
<td><input name="finEDU" type="text" onChange="updatesum(this)" />      </td>
</tr>
<tr>
<td><p>Given to District Mission Fund</p>      </td>
<td><input name="finDMF" type="text" onChange="updatesum(this)" />      </td>
</tr>
<tr>
<td><p><strong>Total Given to Global &amp; District Missions</strong></p>      </td>
</tr>
</table>
</form>
</body>
</html>
``````
0

Author Commented:
I added the script as follows, but now the form does not auto sum at all. Can you advise?
``````<script type="text/javascript">
var theForm = theField.form;
var fldName = "finIncTot|finIncWEF|finAMS|finPENS|finEDU|finDMF".split('|');
var theTotal = 0;
for(var i=0;i<fldName.length;i++){
var theField = theForm[fldName[i]];
var theValue = theField.value.replace(/[^\d\.\-]/g,"");
if(theValue>""){
theTotal = theTotal + (theValue*1);
theField.value = "\$"+(theValue*1).toFixed(2);
} else {
theField.value = "";
}
}
theForm.finTotal.value = "\$"+theTotal.toFixed(2);
}
</script>
<fieldset><legend>Financial Summary</legend>
<table border="0">
<tr>
<td><p>Total Church Income</p>      </td>
<td><span class="style2">\$</span><input name="finIncTot" type="text" id="finIncTot" onChange="updatesum(theField)" onBlur="this.value=formatCurrency(this.value)"/>      </td>
</tr>
<tr>
<td><p>Given to WEF Fund</p>      </td>
<td><span class="style2">\$</span><input name="finIncWEF" type="text" id="finIncWEF" onChange="updatesum(theField)" onBlur="this.value=formatCurrency(this.value)"/>      </td>
</tr>
<tr>
<td><p>Given to Approved Missions Specials</p>      </td>
<td><span class="style2">\$</span><input name="finAMS" type="text" onChange="updatesum(theField)" onBlur="this.value=formatCurrency(this.value)" />      </td>
</tr>
<tr>
<td><p>Given to Pension Fund</p>      </td>
<td><span class="style2">\$</span><input name="finPENS" type="text" onChange="updatesum(theField)" onBlur="this.value=formatCurrency(this.value)" />      </td>
</tr>
<tr>
<td><p>Given to Education Fund</p>      </td>
<td><span class="style2">\$</span><input name="finEDU" type="text" onChange="updatesum(theField)" onBlur="this.value=formatCurrency(this.value)" />      </td>
</tr>
<tr>
<td><p>Given to District Mission Fund</p>      </td>
<td><span class="style2">\$</span><input name="finDMF" type="text" onChange="updatesum(theField)" onBlur="this.value=formatCurrency(this.value)" />      </td>
</tr>
<tr>
<td><p><strong>Total Given to Global &amp; District Missions</strong></p>      </td>
<td><span class="style2">\$</span><input readonly name="finTotal" id="finTotal" onFocus="this.value=formatCurrency(this.value)" class="total" /></td>
</tr>
</table>
``````
0

Systems architectCommented:
Compare your code and my code and you will find the diferences. That process is called: learning.
I gave you perfect running code you want me to search your errors?
Look for the diferences and you will find it.
0

Author Commented:
I cut and pasted your code.
0

Systems architectCommented:
You did cut my code. Look what you missed ;-)
For example only: you missed that my code does have the <form> tag; your doesn't. Without form tag you cannot use DOM form object because it is not on the page. Now you did not learn this lesson yourself and will blame me that something else does not work. For example your code calls formatCurrency() function; where is that function defined?

And most important lessen: do you know what the word "this" in JavaScript means? I tell you like me you will need several iteration until you fully understand the meaning of: this

0

Author Commented:
Wasn't trying to argue, just get some help.

I took a closer look and found my error.  I added (this) to the autosum function and it works as needed.

To clarify my code, this is just 1 piece of a 7 part form all contained in a <form> tag that is set earlier in the document.  Also, the format currency is set in the <head> of my document.  Sorry i didn't make that clearer earlier.
0

Systems architectCommented:
No problem.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.