Hi,
I am new to Javascript and am having a few problems with what are probably very simple issues.
In the attached code how do I prevent “NaN” and “Infinity” error messages in the result1-3 fields.
I have had a go at the isNAN() etc functions but I just cant seem to get the syntax right.
How would I restrict the results to 3 decimal places?
I also want to evalaute the three results and populate the lowest field with a text response indicating which of the results has the lowest value, ie “Result 1 is the lowest value”.
Thanks for any assistance you can offer,
Cheers
Iain
[CODE]
<html>
<head>
<script language=”javascript” type=”text/javascript”>
<!– –>
function cost1()
{
document.calc.result1.value=document.calc.num1.value*100/document.calc.num2.value;
}
function cost2()
{
document.calc.result2.value=document.calc.num3.value*100/document.calc.num4.value;
}
function cost3()
{
document.calc.result3.value=document.calc.num5.value*100/document.calc.num6.value;
}
</script>
</head>
<body>
<form name=”calc”>
<table>
<tr>
<td align=”center”>
<input type=”text”
value=”0″
onfocus=”if(this.value == this.defaultValue) this.value = ””
name=”num1″
size=”10″ style=”text-align: center” />
</td>
<td align=”center”>
<input type=”text”
value=”0″
onfocus=”if(this.value == this.defaultValue) this.value = ””
name=”num2″
size=”10″ style=”text-align: center” />
</td>
<td align=”center”>
<input type=”text”
value=”0″
onfocus=”if(this.value == this.defaultValue) this.value = ””
name=”result1″ size=”14″ style=”text-align: right” />
</td>
</tr>
<tr>
<td align=”center”>
<input type=”text”
value=”0″
onfocus=”if(this.value == this.defaultValue) this.value = ””
name=”num3″
size=”10″ style=”text-align: center” />
</td>
<td align=”center”>
<input type=”text”
value=”0″
onfocus=”if(this.value == this.defaultValue) this.value = ””
name=”num4″ size=”10″
style=”text-align: center” />
</td>
<td align=”center”>
<input type=”text”
value=”0″
onfocus=”if(this.value == this.defaultValue) this.value = ””
name=”result2″
size=”14″ style=”text-align: right” />
</td>
</tr>
<tr>
<td align=”center”>
<input type=”text”
value=”0″
onfocus=”if(this.value == this.defaultValue) this.value = ””
name=”num5″
size=”10″ style=”text-align: center” />
</td>
<td align=”center”>
<input type=”text”
value=”0″
onfocus=”if(this.value == this.defaultValue) this.value = ””
name=”num6″ size=”10″
style=”text-align: center” />
</td>
<td align=”center”>
<input type=”text”
value=”0″
onfocus=”if(this.value == this.defaultValue) this.value = ””
name=”result3″
size=”14″ style=”text-align: right” />
</td>
</tr>
<tr>
<td align=”center”>
<input type=”button” style=”font-size: 12pt; color: #oooooo” value=”Calculate” onClick=”cost1();cost2();cost3()” />
</td>
</tr>
<tr>
<td align=”center”>
<input type=”text” name=”lowest” size=”14″ style=”text-align: center” />
</td>
</tr>
</table>
</body>
</html>