Click to See Complete Forum and Search --> : calcuator


swstos
02-06-2003, 01:05 PM
Hi...again
I have 4 edit boxes and a button...

When you click the button it substracts from the first edit box the values of the second and third and the result is displayed in the fourth edit box.

The code is shown below

<html>
<head>
<title>calculator</title>
<SCRIPT LANGUAGE="JavaScript">
function sub(p1,p2,p3){
return parseInt(p1) - parseInt(p2) - parseInt(p3);
}
</SCRIPT>
</head>
<body>
<p>
<input type="number" value = "" name="first" size="15">
<input type="number" value = "" name="second" size="15">
<input type="number" value = "" name="third" size="15">
</p>
<p>
<input type="button" value=" Total " onClick="TOTALBOX.value= sub(first.value,second.value,third.value);" name="button">
<br>
<input type="text" value = "" name="TOTALBOX" size="15">
</p>
</body>
</html>

It works BUT if i dont insert a value to any of the first three edit boxes... in the total edit box the result is NaN!!!!

How can i do it in order to display a message saying (please insert values to all the edit boxes)

Thanks in advance...

Nevermore
02-06-2003, 01:37 PM
Here is a revision of your script which will pop up an alert and display the text 'Not enough values' if all three boxes are not filled in:

<html>
<head>
<title>calculator</title>
<SCRIPT LANGUAGE="JavaScript">
function sub(p1,p2,p3){
if (p1 < 0 || p2 < 0 || p3 < 0) {
return parseInt(p1) - parseInt(p2) - parseInt(p3);
}
else {
alert("Please insert a value in all three boxes.");
return "Not enough values";

}
}

</SCRIPT>
</head>
<body>
<p>
<input type="number" value = "" name="first" size="15">
<input type="number" value = "" name="second" size="15">
<input type="number" value = "" name="third" size="15">
</p>
<p>
<input type="button" value=" Total " onClick="TOTALBOX.value= sub(first.value,second.value,third.value);" name="button">
<br>
<input type="text" value = "" name="TOTALBOX" size="15">
</p>
</body>
</html>

Zach Elfers
02-06-2003, 01:38 PM
if (document.form.first.value = "" || document.form.second.value = "" || document.form.third.value = "") {
alert("Please fill out the values.");
}

swstos
02-06-2003, 01:42 PM
hi...
what you post earlier cijori...

is not working properly...

when i insert the values in the 3 edit boxes and click the button is giving the error message!!!!

Thanks...

Zach Elfers
02-06-2003, 01:43 PM
Originally posted by cijori
Here is a revision of your script which will pop up an alert and display the text 'Not enough values' if all three boxes are not filled in:

<html>
<head>
<title>calculator</title>
<SCRIPT LANGUAGE="JavaScript">
function sub(p1,p2,p3){
if (p1 < 0 || p2 < 0 || p3 < 0) {
return parseInt(p1) - parseInt(p2) - parseInt(p3);
}
else {
alert("Please insert a value in all three boxes.");
return "Not enough values";

}
}

</SCRIPT>
</head>
<body>
<p>
<input type="number" value = "" name="first" size="15">
<input type="number" value = "" name="second" size="15">
<input type="number" value = "" name="third" size="15">
</p>
<p>
<input type="button" value=" Total " onClick="TOTALBOX.value= sub(first.value,second.value,third.value);" name="button">
<br>
<input type="text" value = "" name="TOTALBOX" size="15">
</p>
</body>
</html>

Why do you have if (p1 < 0 ...). This will only execute if the value of p1 is less than 0. This will not them only be able to enter negative numbers. It should be if (p1 == "") instead.

swstos
02-06-2003, 01:51 PM
The code below is working!!!
Thanks for the help... ;)

<SCRIPT LANGUAGE="JavaScript">
function sub(p1,p2,p3){
if (p1 == "" || p2 == "" || p3 == "") {
alert("Please insert a value in all three boxes.");
return "Not enough values";
}
else {
return parseInt(p1) - parseInt(p2) - parseInt(p3);
}
}
</script>