Solved

Auto Sum form Fields with Currency values

Posted on 2011-09-20
8
1,022 Views
Last Modified: 2012-05-12
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.  

Can anyone advise?

 
<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">
	function updatesum() {
	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>
    <td><input readonly name="finTotal" id="finTotal" onBlur="this.value=formatCurrency(this.value)"/></td>
  </tr>
</table>

Open in new window

0
Comment
Question by:axessJosh
  • 4
  • 4
8 Comments
 
LVL 2

Author Comment

by:axessJosh
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

by:
Zvonko earned 500 total points
ID: 36569968
Here how I would do:
<html>
<head>
<title>Zvonko &#42;</title>
<script type="text/javascript">
function updatesum(theField) {
  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>
</head>
<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>
    <td><input readonly name="finTotal" id="finTotal" /></td>
  </tr>
</table>
</form>
</body>
</html>

Open in new window

0
 
LVL 2

Author Comment

by:axessJosh
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">
function updatesum(theField) {
  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>

Open in new window

0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 63

Expert Comment

by:Zvonko
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

by:axessJosh
ID: 36583958
I cut and pasted your code.
0
 
LVL 63

Expert Comment

by:Zvonko
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

by:axessJosh
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.

Thanks for your help.

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

by:Zvonko
ID: 36588735
No problem.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Today I would like to talk about localizing (Internationalization) JavaScript applications. Introduction When creating an application that is going to be used by many people around the globe, it is important to remember that not everyone speak…
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
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…

790 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question