Link to home
Create AccountLog in
Avatar of cuconsortium
cuconsortiumFlag for United States of America

asked on

Java Script Mask and Summing Up

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!!

 
<body>
<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()" />
 <br>
 
<input name="txt_money2" type="text" id="txt_money2" onblur="this.value=format(this.value)" onchange="TotalCC()" />
<br>
  
<input name="txt_money3" type="text" id="txt_money3" onblur="this.value=format(this.value)" onchange="TotalCC()"/> 
<br>

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

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

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

</form>
</body>

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

ASKER CERTIFIED SOLUTION
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of Proculopsis
Proculopsis


Here's a more simple version:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head>
<title>http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_27044851.html</title>
<script type="text/javascript" src="http://filedb.experts-exchange.com/incoming/2011/05_w20/458595/jquery-1.5.2.min.js"></script>
<script type="text/javascript">

jQuery(document).ready( function() {

  $("input[name^=txt_money]")
  .change( totalCC )
  .change( format );

});

function totalCC() {
  var total = 0.0;
  $("input[name^=txt_money]").each( function() {
    var value = Number( $(this).attr( "raw" ) );
    if ( !isNaN( value ) ) total += value;
    $("#txt_totalCC").val( total );
    format.apply( $("#txt_totalCC") );
  });
}
		 
		 
function format() {  
  num = $(this).val().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();  
 $(this).attr( { raw: num } );
 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))  }  
 $(this).val( (((sign) ? '' : '-') + '$' + num + '.' + cents) );
}
</script>

</head> 
<body> 

<body>
<form id="form1" name="form1" method="post" action="">
  
<input name="txt_money1" type="text" id="txt_money1" />
 <br>
 
<input name="txt_money2" type="text" id="txt_money2" />
<br>
  
<input name="txt_money3" type="text" id="txt_money3" /> 
<br>

<input name="txt_money4" type="text" id="txt_money4" />
<br>
  
<input name="txt_money5" type="text" id="txt_money5" />
<br>
  
<input name="txt_money6" type="text" id="txt_money6" />
<br>
<br>
  
<input name="txt_money7" type="text" id="txt_money7" />
<br>
  
<input name="txt_money8" type="text" id="txt_money8" />
 <br>
  
<input name="txt_money9" type="text" id="txt_money9" />
<br>
  
<input name="txt_money10" type="text" id="txt_money10" />
  <br>
  
<input name="txt_money11" type="text" id="txt_money11" />
<br>

<input name="txt_money12" type="text" id="txt_money12" />
<br>

Total: 
<input name="txt_totalCC" type="text" id="txt_totalCC" />
  <br>

</form>

</body>
</html>

Open in new window

Thanks for the points!
Avatar of cuconsortium

ASKER

Hi Leakim971,

  Unfortunately, I found a bug....

when I add up $1.00 + $1.00 + $1.50 + $0.75 , The total returns me  $4.11
It should be  $6.05

  will you be able to fix it?

Thank you!
You mean $4.25 (and not $6.05)

(String.prototype.cleanNumber corrected)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script language="javascript">

	String.prototype.cleanNumber = function() {
		var x = 1;
		var e = this.indexOf(".");
		if(e>=0) {
			x = Math.pow(10, this.length-e-1);
		}
		var n = this;
		n = n.replace(/\D/g,"");
		var d = n / x;
		if(isNaN(d)) d=0;
		return d;
	}

	function TotalCC() {
		var l=document.form1.txt_money1.value.cleanNumber();
		var a=document.form1.txt_money2.value.cleanNumber(); 
		var b=document.form1.txt_money3.value.cleanNumber(); 
		var c=document.form1.txt_money4.value.cleanNumber(); 
		var d=document.form1.txt_money5.value.cleanNumber(); 
		var e=document.form1.txt_money6.value.cleanNumber(); 

		var f=document.form1.txt_money7.value.cleanNumber(); 
		var g=document.form1.txt_money8.value.cleanNumber(); 
		var h=document.form1.txt_money9.value.cleanNumber(); 
		var i=document.form1.txt_money10.value.cleanNumber(); 
		var j=document.form1.txt_money11.value.cleanNumber(); 
		var k=document.form1.txt_money12.value.cleanNumber(); 
		 
		var totalCC;
		 
		totalCC = l + a + b + c + d + e + f + g + h + i + j + k;
		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)	
	}

</script>
</head>

<body>
<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()"/><br />
    <input name="txt_money2" type="text" id="txt_money2" onblur="this.value=format(this.value)" onchange="TotalCC()"/><br />
    <input name="txt_money3" type="text" id="txt_money3" onblur="this.value=format(this.value)" onchange="TotalCC()"/><br />
    <input name="txt_money4" type="text" id="txt_money4" onblur="this.value=format(this.value)" onchange="TotalCC()"/><br />
    <input name="txt_money5" type="text" id="txt_money5" onblur="this.value=format(this.value)" onchange="TotalCC()"/><br />
    <input name="txt_money6" type="text" id="txt_money6" onblur="this.value=format(this.value)" onchange="TotalCC()"/><br />
    <br />
    <input name="txt_money7" type="text" id="txt_money7" onblur="this.value=format(this.value)" onchange="TotalCC()"/><br />
    <input name="txt_money8" type="text" id="txt_money8" onblur="this.value=format(this.value)" onchange="TotalCC()"/><br />
    <input name="txt_money9" type="text" id="txt_money9" onblur="this.value=format(this.value)" onchange="TotalCC()"/><br />
    <input name="txt_money10" type="text" id="txt_money10" onblur="this.value=format(this.value)" onchange="TotalCC()"/><br />
    <input name="txt_money11" type="text" id="txt_money11" onblur="this.value=format(this.value)" onchange="TotalCC()"/><br />
    <input name="txt_money12" type="text" id="txt_money12" onblur="this.value=format(this.value)" onchange="TotalCC()"/><br />
    Total: 
    <input name="txt_totalCC" type="text" id="txt_totalCC" disabled="disabled"/>
</form>
</body>
</html>

Open in new window

yup, $4.25.

It looks right now!  Thanks~!  What was wrong with it?

parseInt(this.replace(/\D/g,""))/x return a wrong number
Yah, I was wondering about the parseInt at the first glance.  However, Thank you!
You're welcome!