Here's a way in JavaScript you might be able to adapt. If you have an HTML editor such as 1st Page 2000 (free athttp://www.evrsoft.com/download)you can paste this code into a new blank document, replace all occurances of < with < and all occurances of > with > and save it as test.htm you'll be able to see how it is working and modify it to suit your needs.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Add Two Numbers</title>
<script language="JavaScript">
function NumOnly(event) {
<!-- Make sure only numbers or a decimal point are entered //-->
var charCode = (event.keyCode)
if (charCode > 31 && (charCode < 48 || charCode > 57)) {
if (charCode !=46) {
return false;
}
}
}
function calculate() {
<!-- Check if first box was left empty //-->
if (document.myform.firstnumber.value == "" ) {
<!-- If so, focus on first box //-->
myform.firstnumber.focus();
<!-- and exit this function //-->
return false;
}
<!-- Check if second box was left empty //-->
if (document.myform.secondnumber.value == "" ) {
<!-- If so, focus on second box //-->
myform.secondnumber.focus();
<!-- and exit this function //-->
return false;
}
<!-- Get first number from the form (named 'myform') //-->
var a=parseFloat(document.myform.firstnumber.value);
<!-- Get second number from the form (named 'myform') //-->
var b=parseFloat(document.myform.secondnumber.value);
<!-- DO CALCULATION HERE - Here, a is added to b to give c //-->
var c=a+b;
<!-- Display some text, then the answer to the sum, then a reset button in the 'answer' span //-->
document.getElementById("answer" ).innerHTML = '<p>'+a+' + '+b+' = '+c+'</p><input type="button" value="Reset" onClick="resetform();">';
}
function resetform() {
<!-- Clear form //-->
document.myform.reset();
<!-- Change text in the 'answer' span //-->
document.getElementById("answer" ).innerHTML = '<p>Enter two numbers & click <b>Answer</b>.</p><INPUT type="button" value="Answer" onClick="calculate();">';
<!-- Focus on first box //-->
myform.firstnumber.focus();
}
</script>
</head>
<body>
<table width="300" cellpadding="0" cellspacing="0" border="0">
<tr>
<td>
<FORM name="myform">
<INPUT name="firstnumber" size="20" onkeypress="return NumOnly(event);"><br><br>
<INPUT name="secondnumber" size="20" onkeypress="return NumOnly(event);"><br><br>
<span id="answer">
<p>Enter two numbers & click <b>Answer</b>.</p>
<INPUT type="button" value="Answer" onClick="calculate();">
</span>
</FORM>
</td>
</tr>
</table>
</body>
</html>
Edit:
Just incase that's hard to follow, here's a simplified version of the same thing. It won't filter unwanted characters or swap buttons and clear the fields, etc., but with all that taken out it might be clearer:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Add Two Numbers</title>
<script language="JavaScript">
function calculate() {
var a=parseFloat(document.myform.firstnumber.value);
var b=parseFloat(document.myform.secondnumber.value);
var c=a+b;
document.getElementById("answer" ).innerHTML = c;
}
</script>
</head>
<body>
<table width="300" cellpadding="0" cellspacing="0" border="0">
<tr>
<td>
<FORM name="myform">
<INPUT name="firstnumber" size="20"><br><br>
<INPUT name="secondnumber" size="20"><br><br>
<INPUT type="button" value="Answer" onClick="calculate();">
</FORM>
<span id="answer"></span>
</td>
</tr>
</table>
</body>
</html>
Does that cover what you need to do?
(Edited by KC at 1:41 am on April 9, 2003)