Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Automatic calculation in text fields 1

Status
Not open for further replies.

ateo

Programmer
May 23, 2003
4
US
Hi,

Can someone help me with this task:

I have four textboxes. I would like the calculation result (addition only) to appear automatically in textbox 5. How do I do it?

Also how do I avoid addition of a negative number?

Thanks for any help!
 
first calcuations use the parseInt and parseFloat functions for data type conversions before that arrises in the script.

you can use key events or a onBlur of the forth textbox to run a function auto (dynamicaly)

eg:
<script language=&quot;javascript&quot;>
function hi() {
alert(&quot;hello!&quot;);
}
</script>

<input type=&quot;text&quot; name=&quot;txt&quot; onBlur=&quot;hi()&quot;>

when the box loses focus the function runs.

you can see the key events here

along with the onBlur referenced a bit more

_________________________________________________________
$str = &quot;sleep is good for you. sleep gives you the energy you need to function&quot;;
$Nstr = ereg_replace(&quot;sleep&quot;,&quot;coffee&quot;,$str); echo $Nstr;

onpnt2.gif
[/sub]
 
Try this:

Code:
function sum_boxes(callfrom)
{
 var e = document.formname.length;
 var tot=0;
 var i;
 for (i=0; i < e; i++)
 {
  // scan all form elements to find what we need
  var n=document.formname.elements[i].name;
  var v=document.formname.elements[i].value;
  if (n.substr(0,7) == &quot;textbox&quot; && v.length != 0)
  {
   if (0 < v)
   {
    tot += parseFloat(v)
   }
  }
 }
 document.formname.textbox5.value=tot
}

I recommend parseFloat() since it does not parse numbers entered with preceeding zeros as octal values. Also, you have to keep the exact captialization in JavaScript. The function parseFloat() works but not parsefloat().

For textboxes 1-4 you can use <INPUT ... ONBLUR=&quot;return sum_boxes('textbox')&quot;>. I added the parameter only in case you wanted to test differently at the entrybox level versus the submit level.

Also, for textbox5 use noedit=&quot;noedit&quot; in the <INPUT> tag so that the user cannot edit the sum displayed.
 
dbMark,
This is what I did:

<script language=&quot;JavaScript&quot; type=&quot;text/javascript&quot;>

function sum_boxes(callfrom)
{
var e = document.theform.length;
var tot=0;
var i;
for (i=0; i < e; i++)
{
// scan all form elements to find what we need
var n=document.theform.elements.name;
var v=document.theform.elements.value;
if (n.substr(0,7) == &quot;textbox&quot; && v.length != 0)
{
if (0 < v)
{
tot += parseFloat(v)
}
}
}
document.theform.Total.value=tot
}
</script>

...

<input type=&quot;text&quot; name=&quot;value1&quot; size=&quot;10&quot; class=&quot;edit&quot;
onblur=&quot;return sum_boxes(&quot;textbox&quot;)&quot;><br>
<input type=&quot;text&quot; name=&quot;value2&quot; size=&quot;10&quot; class=&quot;edit&quot;
onblur=&quot;return sum_boxes(&quot;textbox&quot;)&quot;><br>
<input type=&quot;text&quot; name=&quot;value3&quot; size=&quot;10&quot; class=&quot;edit&quot;
onblur=&quot;return sum_boxes(&quot;textbox&quot;)&quot;><br>
<input type=&quot;text&quot; name=&quot;value4&quot; size=&quot;10&quot; class=&quot;edit&quot;
onblur=&quot;return sum_boxes(&quot;textbox&quot;)&quot;><br>
<input class=&quot;edit&quot; type=&quot;text&quot; name=&quot;Total&quot; noedit=&quot;noedit&quot; size=&quot;10&quot;>

...


The value did not add up. It just showed &quot;0&quot; in textbox 5-- &quot;Total&quot;. I don't know which part I did wrong. Any help is greatly appreciated! Thanks.
 
dbMark,

are you aware that you can specify the radix with parseInt() to prevent &quot;025&quot; from being treated as octal?

parseInt(&quot;025&quot;, 10);

the radix you specify determines how the number is treated...
parseInt(&quot;11&quot;, 2)
&quot;11&quot; will be treated as binary and will return 3.



=========================================================
try { succeed(); } catch(E) { tryAgain(); }
-jeff
 
ateo: One reason why it didn't work is that you can't nest double quotes like that. Try nesting single quotes inside the double quotes this way and try again:

onblur=&quot;return sum_boxes('textbox')&quot;

You may not need to send any parameter if you're only using the code one way. I included it just in case you wanted to handle the individual textboxes slightly differently from the SUBMIT logic.

jemminger: Thanks for the explanation. I have a huge book which does NOT show that &quot;radix&quot; parameter for parseInt() so I didn't even know what a radix was or what the 10 meant. I had guessed wrongly that it was 10 digits or spaces, oops. I'll write it in the margin with all my other notes. Did cryptographers write Javascript?
 
dbMark,

maybe time for a new reference...i use o'reilly's &quot;javascript: the definitive guide&quot;

no cryptographers - just programmers, and you know how well we document and comment our own code, right? ;-)

=========================================================
try { succeed(); } catch(E) { tryAgain(); }
-jeff
 
dbMark,

Thanks for the response.

I changed the double quote to single quote but it still won't work. Textbox 5 always show 0 whenever I put in value in textbox 1-4.

Thanks for your help.
 
ateo: I see that the names of your textboxes are value1,2,3,4. In the sample I gave I looped through all form elements looking for only variables whose names started with &quot;textbox&quot; such as textbox1, textbox2, etc. You are using names starting with &quot;value&quot;. In that case, you need to search for the name you are using. By the way, for this reason none of the other form elements should begin with the same letters so you don't mess up the total routine.

replace:
Code:
if (n.substr(0,7) == &quot;textbox&quot; && v.length != 0)
with:
Code:
if (n.substr(0,5) == &quot;value&quot; && v.length != 0)

JavaScript is very picky and browsers seldom tell you where the problem is. Just never give up. Sometimes I find that when I make one small change, I spend a lot of time making the rest of the code work with that change. The nature of the beast...

One other note, while posting all these messages, I learned something too, that these 2 lines will give the same results:

Code:
tot += parseFloat(v);
tot += parseInt(v,10);

At first the &quot;parseInt(v)&quot; code didn't always work as expected for me since entries beginning with a zero such as &quot;0123&quot; were being interpreted as octal numbers in base 8. I didn't realize that and so I didn't mention it earlier. But since then I was told that adding another parameter, a &quot;radix&quot;, into the function so that it was &quot;parseInt(v,10)&quot; forced the value to be interpreted as base 10, our everyday decimal system. Another problem solved.

jemminger: Too bad my steeply discounted book forgot to include that little detail on parseInt()'s extra parameter. It's a 2001 $49.99 book with 1079 pages and named &quot;Javascript: The Complete Reference&quot; by Osborne/McGraw-Hill. This is the second error or omission I've found in the book. The other was on page 430 where the &quot;maxlength&quot; INPUT property should be &quot;maxLength&quot;.
 
It works now.
Thanks, dbMark. You da man! :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top