Solved

# Auto Sum form Fields with Currency values

Posted on 2011-09-20
1,051 Views
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
Question by:axessJosh
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 4
• 4

LVL 2

Author Comment

ID: 36569700
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

LVL 63

Accepted Solution

Zvonko earned 500 total points
ID: 36569968
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

LVL 2

Author Comment

ID: 36583695
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

LVL 63

Expert Comment

ID: 36583914
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

LVL 2

Author Comment

ID: 36583958
I cut and pasted your code.
0

LVL 63

Expert Comment

ID: 36585328
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

LVL 2

Author Comment

ID: 36587486
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

LVL 63

Expert Comment

ID: 36588735
No problem.
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
###### Suggested Courses
Course of the Month7 days, 11 hours left to enroll