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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

This works except if i say leave t3 empty int4 it will display NaN 1

Status
Not open for further replies.

data1

Programmer
Aug 19, 2003
25
0
0
NZ
<HTML>
<HEAD>
<TITLE>Untitled</TITLE>
<SCRIPT language=&quot;JavaScript&quot;>
function calculate(){
var f = document.form1;
var first = f.t1.value;
var second = f.t2.value;
var third = f.t3.value;
f.t4.value = parseFloat(first) + parseFloat(second) + parseFloat(third);
}
</SCRIPT>
</HEAD>
<BODY>
<FORM name=&quot;form1&quot;>
<TABLE align=&quot;center&quot; cellspacing=&quot;2&quot; cellpadding=&quot;2&quot; border=&quot;0&quot;>
<TR>
</TABLE>
<TR>
<TD>Inputbox 1
<INPUT type=&quot;text&quot; name=&quot;t1&quot;>
</TD>
<TD>Input box2
<INPUT type=&quot;text&quot; name=&quot;t2&quot;>
</TD>
<TD>Input box3
<INPUT type=&quot;text&quot; name=&quot;t3&quot;>
</TD>

<TD>Result=
<INPUT type=&quot;text&quot; name=&quot;t4&quot; onfocus=&quot;document.form1.t4.blur()&quot; readOnly=&quot;true&quot;>
</TD>
<TD>
<INPUT type=&quot;button&quot; value=&quot;Calculate&quot; onclick=&quot;return calculate()&quot;>
</TD>
</TR>
</TABLE>
</FORM>
</BODY>
</HTML>//If say only two text boxes are filled out and the person dose not need to fill in the third one I get NaN as a result in text box Four
 
check to make sure the value is not blank before assigning to first second or third. If any are blank set them to 0 before adding them

on error goto hell
 
data1,

Would it be ok to asign 0 to the text box by default?
If so just do this to all text boxes.. t1 and t2 and t3..

<INPUT type=&quot;text&quot; name=&quot;t1&quot; value=&quot;0&quot;>
<INPUT type=&quot;text&quot; name=&quot;t2&quot; value=&quot;0&quot;>
<INPUT type=&quot;text&quot; name=&quot;t3&quot; value=&quot;0&quot;>

If this will not work for you let us know and we can come up with another solution.

You may also need to validate the text box to make sure that what the user enters is a number and not an alphabetic character. (There are many threads in this forum that can help you validate the entry) you can do a search to find examples of it.

Sorry to be so short, I have to head out to work.

Brian

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top