codice:
<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Esempio</title>
<script type="text/javascript">
function binaryCheck (oToCheckField, oKeyEvent) {
return oKeyEvent.charCode === 0 || /[01]/.test(String.fromCharCode(oKeyEvent.charCode));
}
function subtraction (oForm) {
var
nFirst = parseInt(oForm.first1.value + oForm.first2.value + oForm.first3.value + oForm.first4.value, 2),
nSecond = parseInt(oForm.second1.value + oForm.second2.value + oForm.second3.value + oForm.second4.value, 2),
sResult = ("000" + (Math.abs(nFirst - nSecond)).toString(2)).substr(-4);
for (var nIdx = 1; nIdx < 5; oForm["result" + nIdx].value = sResult.charAt(nIdx++ - 1));
}
</script>
</head>
<body>
<form name="form1">
<input type="text" onkeypress="return binaryCheck(this, event);" onpaste="return false;" size="1" name="first1" value="0" >
<input type="text" onkeypress="return binaryCheck(this, event);" onpaste="return false;" size="1" name="first2" value="0" >
<input type="text" onkeypress="return binaryCheck(this, event);" onpaste="return false;" size="1" name="first3" value="0" >
<input type="text" onkeypress="return binaryCheck(this, event);" onpaste="return false;" size="1" name="first4" value="0" >-
<input type="text" onkeypress="return binaryCheck(this, event);" onpaste="return false;" size="1" name="second1" value="0" >
<input type="text" onkeypress="return binaryCheck(this, event);" onpaste="return false;" size="1" name="second2" value="0" >
<input type="text" onkeypress="return binaryCheck(this, event);" onpaste="return false;" size="1" name="second3" value="0" >
<input type="text" onkeypress="return binaryCheck(this, event);" onpaste="return false;" size="1" name="second4" value="0" >=
<hr>
<input type="text" readonly size="1" name="result1" value="0">
<input type="text" readonly size="1" name="result2" value="0">
<input type="text" readonly size="1" name="result3" value="0">
<input type="text" readonly size="1" name="result4" value="0">
<input value="calcola" onclick="subtraction(this.form);" type="button">
</form>
</body>
</body>
</html>