Simple Math...yea right

1. Senior Member
Join Date
Dec 1969
Posts
447

## Simple Math...yea right

I have two fields that I am formatting to currency onblur. I think that is working correctly. The third field is for the total. I have a total button that calls a script to total the two fields. It only works if the numbers in box a and b are numeric, containing no ","&#039;s 1,234.25 which the format currency script adds. The addition sees this as a string and gives me a NAN.<BR><BR>I am not sure where to begin here. I have looked around at some examples and get very confused. Below is what I have so far, any help would be appreciated.<BR><BR>&#060;html&#062;<BR>&#060;head &#062;<BR>&#060;title&#062;Math&#060;/title&#062;<BR>&#060;meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"&#062;<BR>&#060;script&#062;<BR>function ttlCurrency(){<BR> var a = document.frm1.f1.value;<BR> var b = document.frm1.f2.value;<BR> <BR> var c = (a-0) + (b-0);<BR><BR> document.frm1.f3.value = c;<BR>}<BR>&#060;/script&#062;<BR>&#060;SCRIPT LANGUAGE="JavaScript"&#062;<BR>&#060;!--<BR>&#060;!-- Begin<BR>function formatCurrency(num) {<BR>num = num.toString().replace(/\$&#124,/g,&#039;&#039;);<BR>if(isNaN(num))<BR>num = "0";<BR>sign = (num == (num = Math.abs(num)));<BR>num = Math.floor(num*100+0.50000000001);<BR>cents = num%100;<BR>num = Math.floor(num/100).toString();<BR>if(cents&#060;10)<BR>cents = "0" + cents;<BR>for (var i = 0; i &#060; Math.floor((num.length-(1+i))/3); i++)<BR>num = num.substring(0,num.length-(4*i+3))+&#039;,&#039;+<BR>num.substring(num.lengt h-(4*i+3));<BR>return (((sign)?&#039;&#039;:&#039;-&#039;) + num + &#039;.&#039; + cents);<BR>//OLD LINE - return (((sign)?&#039;&#039;:&#039;-&#039;) + &#039;\$&#039; + num + &#039;.&#039; + cents);<BR>}<BR>// End --&#062;<BR>//--&#062;<BR>&#060;/script&#062;<BR><BR>&#060;/head&#062;<BR><BR>&#060;body&#062;<BR>&#060;form action="" method="post" name="frm1" id="frm1"&#062;<BR> &#060;input type="text" name="f1" onBlur="this.value=formatCurrency(this.value);"&#0 62;<BR> <BR><BR> &#060;input type="text" name="f2" onBlur="this.value=formatCurrency(this.value);"&#0 62;<BR> <BR><BR> <BR><BR> &#060;input type="text" name="f3" onBlur="this.value=formatCurrency(this.value);"&#0 62;<BR> &#060;input type="button" name="Button" value="Get Total" onClick="javascript:ttlCurrency();"&#062;<BR>&#060 ;/form&#062;<BR><BR><BR>&#060;/body&#062;<BR>&#060;/html&#062;

2. Senior Member
Join Date
Dec 1969
Posts
96,118

## Use a regular expression...

...to get rid of extraneous characters. Won&#039;t guarantee that somebody won&#039;t enter<BR> 1.2.3.4.5<BR>or something silly, but gives you a fighting chance.<BR><BR>var <BR>function ttlCurrency(){<BR> var okay = /[^0-9.-]/<BR> var a = document.frm1.f1.value;<BR> var b = document.frm1.f2.value;<BR> a = parseFloat( a.replace( okay, "" ) );<BR> b = parseFloat( b.replace( okay, "" ) );<BR> if ( isNaN(a) &#124&#124 isNaN(b) )<BR> {<BR> alert("One or both those fields are not valid numbers.");<BR> document.frm1.f3.value = "-- none --";<BR> } else {<BR> document.frm1.f3.value = formatCurrency( a + b );<BR> }<BR>}<BR><BR>Just one way to do it.<BR><BR>

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•