troubleshooting Question

Java Script Mask and Summing Up

Avatar of cuconsortium
cuconsortiumFlag for United States of America asked on
9 Comments1 Solution518 ViewsLast Modified:
Dear all,

  I have 12 text fields, which need to be masked to currency format, and one text field, which is adding up these 12 text fields and display the Total as currency format.  

  My TotalCC()  Java Script function was working fine until I apply my Format(num) Java Script function to mask the 12 money fields into this format:  $0.50 or $1,000,000.50  

 When I only entered the 1st money field, the Total field adds up the amount correctly, but displays the number without the mask.  I then go ahead entered the 2nd money field, the Total field becomes  "NaN"

 How do I make it work so that the Total field will sum up correctly and display with my currency mask?

 I'm attaching the functions and the HTML codes as follow.

Thank you!!

<form id="form1" name="form1" method="post" action="">
<input name="txt_money1" type="text" id="txt_money1" onblur="this.value=format(this.value)" onchange="TotalCC()" />
<input name="txt_money2" type="text" id="txt_money2" onblur="this.value=format(this.value)" onchange="TotalCC()" />
<input name="txt_money3" type="text" id="txt_money3" onblur="this.value=format(this.value)" onchange="TotalCC()"/> 

<input name="txt_money4" type="text" id="txt_money4" onblur="this.value=format(this.value)" onchange="TotalCC()"/>
<input name="txt_money5" type="text" id="txt_money5" onblur="this.value=format(this.value)" onchange="TotalCC()"/>
<input name="txt_money6" type="text" id="txt_money6" onblur="this.value=format(this.value)" onchange="TotalCC()"/>
<input name="txt_money7" type="text" id="txt_money7" onblur="this.value=format(this.value)" onchange="TotalCC()"/>
<input name="txt_money8" type="text" id="txt_money8" onblur="this.value=format(this.value)" onchange="TotalCC()"/>
<input name="txt_money9" type="text" id="txt_money9" onblur="this.value=format(this.value)" onchange="TotalCC()"/>
<input name="txt_money10" type="text" id="txt_money10" onblur="this.value=format(this.value)" onchange="TotalCC()"/>
<input name="txt_money11" type="text" id="txt_money11" onblur="this.value=format(this.value)" onchange="TotalCC()"/>

<input name="txt_money12" type="text" id="txt_money12" onblur="this.value=format(this.value)" onchange="TotalCC()"/>

<input name="txt_totalCC" type="text" id="txt_totalCC" onchange="TotalCC();this.value=format(this.value)"/>


Open in new window

function TotalCC()
 var l=document.form1.txt_money1.value; 
 var a=document.form1.txt_money2.value; 
 var b=document.form1.txt_money3.value; 
 var c=document.form1.txt_money4.value; 
 var d=document.form1.txt_money5.value; 
 var e=document.form1.txt_money6.value; 
 var f=document.form1.txt_money7.value; 
 var g=document.form1.txt_money8.value; 
 var h=document.form1.txt_money9.value; 
 var i=document.form1.txt_money10.value; 
 var j=document.form1.txt_money11.value; 
 var k=document.form1.txt_money12.value; 
 var totalCC;
 totalCC =  (l*1) + (a*1) + (b*1) + (c*1) + (d*1) + (e*1)+ (f*1)+ (g*1)+ (h*1)+ (i*1)+ (j*1)+ (k*1)
 document.form1.txt_totalCC.value = totalCC
function format(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)


Open in new window

Join our community to see this answer!
Unlock 1 Answer and 9 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 9 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros