Created September 29 1999; Modified October 2002

Auto-Calculator

=

Source Code: JavaScript

<script type="text/javascript" language="Javascript">
<!--//
function validateLegalNumberCharacters(inString){ 

var myLen=inString.length-1;
var validChar='0123456789.';

for ( var i=0;i<myLen;i++ )
    {
    if (validChar.indexOf(inString.substring(i,i+1)) == -1)
        {
            alert('Please enter only numbers.');
            break;
    } else {
        doTheMath();
    }
    }
}

function doTheMath() {
    mathForm = document.fm;
    
    a = mathForm.num_one.value;
    p = mathForm.wc.options[mathForm.wc.selectedIndex].value;
    b = mathForm.num_two.value;
    c = eval(a+p+b+'');
    
    mathForm.num_answer.value=c;
}
//-->
</script>

Source Code: HTML

<form name="fm" action="./" onsubmit="return false;">
<table width="100%" cellpadding="15" cellspacing="0" border="0">
<tr><td align="center">
<input name="num_one" value="2"
    onchange="validateLegalNumberCharacters(this.value);" class="Big" size="6">
<select name="wc" onchange="doTheMath();" class="Big">
    <option value="+" checked>  +  </option>
    <option value="-">  -  </option>
    <option value="/">  /  </option>
    <option value="*">  *  </option>
</select>
<input name="num_two" value="3"
    onchange="validateLegalNumberCharacters(this.value);" class="Big" size="6">
<big class="Big">=</big>
<input name="num_answer" value="5"
    onfocus="document.fm.num_one.focus();" class="Big" size="6">
</td></tr></table>
</form>