Emptrix Staff Administrator
Posts : 1218 Points : 1916 Reputation : 13 Join date : 2009-05-09 Age : 33 Location : The World That Never Was
| Subject: (Requested) Calculator Code - HTML & JavaScript Mon Nov 15, 2010 11:57 am | |
| As requested by burma, here: - Code:
-
<!Author: Emptrix>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"> <html> <head> <meta name="generator" content= "HTML Tidy for Windows (vers 14 February 2006), see www.w3.org"> <title>Welcome</title> <script type="text/javascript"> function calculate() { var n1 = document.form1.num1_txt.value; var n2 = document.form1.num2_txt.value; if (isNaN(n1)) { alert("Number 1 must be a number."); document.form1.num1_txt.focus(); document.form1.num1_txt.value = ""; } else if (isNaN(n2)) { alert("Number 2 must be a number."); document.form1.num2_txt.focus(); document.form1.num2_txt.value = ""; } else if (n2 == "") { alert("Number 2 is required."); document.form1.num2_txt.focus(); document.form1.num2_txt.value = ""; } else if (n1 == "") { alert("Number 1 is required."); document.form1.num1_txt.focus(); document.form1.num1_txt.value = ""; } var result = parseInt(n1) + parseInt(n2);
document.form1.result_txt.value=result; } </script> </head> <body> <form name="form1" allow="" method="post"> <table> <tr> <td>Enter Number 1:</td> <td><input type="text" name="num1_txt" value="" /></td> </tr> <tr> <td>Enter Number 2:</td> <td><input type="text" name="num2_txt" value="" /></td> </tr> <tr> <td>Result:</td> <td><input type="text" name="result_txt" value="" readonly="readonly" /></td> </tr> <tr> <td><input type="button" name="button" value="calculate" onClick="calculate()" /></td> </tr> </table> </form>
<textarea rows="20" cols="150"> <script type="text/javascript"> function calculate() { var n1 = document.form1.num1_txt.value; var n2 = document.form1.num2_txt.value; if (isNaN(n1)) { alert("Number 1 must be a number."); document.form1.num1_txt.focus(); document.form1.num1_txt.value = ""; } else if (isNaN(n2)) { alert("Number 2 must be a number."); document.form1.num2_txt.focus(); document.form1.num2_txt.value = ""; } else if (n2 == "") { alert("Number 2 is required."); document.form1.num2_txt.focus(); document.form1.num2_txt.value = ""; } else if (n1 == "") { alert("Number 1 is required."); document.form1.num1_txt.focus(); document.form1.num1_txt.value = ""; } var result = parseInt(n1) + parseInt(n2);
document.form1.result_txt.value=result; } </script> </head> <body> <form name="form1" allow="" method="post"> <table> <tr> <td>Enter Number 1:</td> <td><input type="text" name="num1_txt" value="" /></td> </tr> <tr> <td>Enter Number 2:</td> <td><input type="text" name="num2_txt" value="" /></td> </tr> <tr> <td>Result:</td> <td><input type="text" name="result_txt" value="" readonly="readonly" /></td> </tr> <tr> <td><input type="button" name="button" value="calculate" onClick="calculate()" /></td> </tr> </table> </form>
</textarea> </body> </html>
Last edited by Emptrix on Tue Nov 16, 2010 12:17 pm; edited 1 time in total | |
|