T
TSK
I cannot get my focus() fn to work in my validateInput(userInput) fn. It
will not allow the user to go back and correct invalid input like it should.
Instead it goes right on through with the invalid input. I used this same
function in another program similar to this one and it worked just fine, but
it's not working in this one. Why? My code is included below. Your help
will be greatly appreciated.
<html>
<head>
<title>Syster Tara's Number Converter</title>
<script language="JavaScript">
var bitString="";
var hexString=""; // 6
function validateInput(userInput) // 8
{
if (isNaN(userInput)) // 10
{
alert("That's not a number. Try again."); // 12
converter.numEntered.focus(); // 13
}
else if (userInput.indexOf(".") != -1) // 15
{
alert("That's not a whole number. Try again."); // 17
converter.numEntered.focus(); // 18
}
else if (parseInt(userInput) < 0) // 20
{
alert("That's a negative number. Try a positive one"); // 22
converter.numEntered.focus(); // 23
}
else
{
return userInput;
}
}
function determineBinStartPower(num) // 38
{
var power, multiple;
num = validateInput(num);
num = parseInt(num); // 40
alert("num = " + num); // 41
power = Math.floor(Math.log(num)/Math.log(2)); // 43
multiple = Math.pow(2, power); // 44
bitString = bitString + "1"; // 45
alert("power = " + power + "\nmultiple = " + multiple + "\nbitString = " +
bitString);
num = num - multiple; // 47
alert("num = " + num); // 48
power--; // 49
determineBinBitVal(num, power); // 51
}
function determineBinBitVal(leftover, exponent) // 54
{
var product; // 56
alert("leftover = " + leftover + "\nexponent = " + exponent + "\nproduct =
" + product);
for(; exponent >= 0; exponent--) // 58
{
product = Math.pow(2, exponent); // 60
if (leftover >= product) // 61
{
bitString = bitString +"1"; // 63
leftover = leftover - product; // 64
alert("leftover = " + leftover + "\nexponent = " + exponent + "\nproduct = "
+ product +
"\nbitString = " + bitString); // 66
}
else
{
bitString = bitString + "0"; // 70
alert("leftover = " + leftover + "\nexponent = " + exponent + "\nproduct = "
+ product +
"\nbitString = " + bitString);
}
}
document.write("The binary value of " + converter.numEntered.value + " is
" + bitString);
}
function determineHexStartPower(num)
{
var power, multiple, quotient, firstDigit;
num = parseInt(validateInput(num));
alert("num = " + num);
power = Math.floor(Math.log(num)/Math.log(16));
multiple = Math.pow(16, power);
quotient = Math.floor(num / multiple);
firstDigit = determineHexDigit(quotient);
hexString = hexString + firstDigit.toString();
alert("power = " + power + "\nmultiple = " + multiple + "\nhexString = " +
hexString);
num = num - (multiple * quotient);
alert("num = " + num);
power--;
determineHexPlaceVal(num, power);
}
function determineHexPlaceVal(leftover, exponent)
{
var product, placeVal, hexDigit;
alert("leftover = " + leftover + "\nexponent = " + exponent + "\nproduct =
" + product);
for(; exponent >= 0; exponent--)
{
product = Math.pow(16, exponent);
alert("product = " + product + " after using the Math.pow fn");
if (leftover >= product)
{
placeVal = Math.floor(leftover / product);
alert("placeVal = " + placeVal + "\nleftover = " + leftover +
"\nproduct = " + product);
hexDigit = determineHexDigit(placeVal);
alert("hexDigit = " + hexDigit);
hexString = hexString + hexDigit.toString();
leftover = leftover - (product * placeVal);
alert("leftover = " + leftover + "\nexponent = " + exponent + "\nproduct = "
+ product +
"\nhexString = " + hexString);
}
else
{
hexString = hexString + "0";
alert("leftover = " + leftover + "\nexponent = " + exponent + "\nproduct = "
+ product +
"\nhexString = " + hexString);
}
}
document.write("The hexadecimal value of " + converter.numEntered.value +
" is " + hexString);
}
function determineHexDigit(num)
{
alert("num = " + num);
switch (num)
{
case 10:
return "A";
break;
case 11:
return "B";
break;
case 12:
return "C";
break;
case 13:
return "D";
break;
case 14:
return "E";
break;
case 15:
return "F";
break;
default:
return num;
}
}
</script>
</head>
<body>
<h1 align="center">Syster Tara's Number Converter</h1>
<h3>Please enter a positive integer into the text box and click either the
left button to get the
number's binary value or the right button to get its hexadecimal value</h3>
<form name="converter">
<table border="0" cellpadding="5" cellspacing="5">
<tr>
<td><a href="http://www.spacefem.com/blobs/"><img
src="http://www.maethos.info/~spacefem/pinkblob.gif" width="90" height="98"
border="0" alt="Adopt
your own useless blob!"></a></td>
<td><input type="text" name="numEntered"></td>
<td><input type="button" name="getBin" value="Get Binary Value"
onclick="determineBinStartPower(numEntered.value)"></td>
<td><input type="button" name="getHex" value="Get Hexadecimal Value"
onclick="determineHexStartPower(numEntered.value)"></td>
</tr>
</table>
</body>
</html>
will not allow the user to go back and correct invalid input like it should.
Instead it goes right on through with the invalid input. I used this same
function in another program similar to this one and it worked just fine, but
it's not working in this one. Why? My code is included below. Your help
will be greatly appreciated.
<html>
<head>
<title>Syster Tara's Number Converter</title>
<script language="JavaScript">
var bitString="";
var hexString=""; // 6
function validateInput(userInput) // 8
{
if (isNaN(userInput)) // 10
{
alert("That's not a number. Try again."); // 12
converter.numEntered.focus(); // 13
}
else if (userInput.indexOf(".") != -1) // 15
{
alert("That's not a whole number. Try again."); // 17
converter.numEntered.focus(); // 18
}
else if (parseInt(userInput) < 0) // 20
{
alert("That's a negative number. Try a positive one"); // 22
converter.numEntered.focus(); // 23
}
else
{
return userInput;
}
}
function determineBinStartPower(num) // 38
{
var power, multiple;
num = validateInput(num);
num = parseInt(num); // 40
alert("num = " + num); // 41
power = Math.floor(Math.log(num)/Math.log(2)); // 43
multiple = Math.pow(2, power); // 44
bitString = bitString + "1"; // 45
alert("power = " + power + "\nmultiple = " + multiple + "\nbitString = " +
bitString);
num = num - multiple; // 47
alert("num = " + num); // 48
power--; // 49
determineBinBitVal(num, power); // 51
}
function determineBinBitVal(leftover, exponent) // 54
{
var product; // 56
alert("leftover = " + leftover + "\nexponent = " + exponent + "\nproduct =
" + product);
for(; exponent >= 0; exponent--) // 58
{
product = Math.pow(2, exponent); // 60
if (leftover >= product) // 61
{
bitString = bitString +"1"; // 63
leftover = leftover - product; // 64
alert("leftover = " + leftover + "\nexponent = " + exponent + "\nproduct = "
+ product +
"\nbitString = " + bitString); // 66
}
else
{
bitString = bitString + "0"; // 70
alert("leftover = " + leftover + "\nexponent = " + exponent + "\nproduct = "
+ product +
"\nbitString = " + bitString);
}
}
document.write("The binary value of " + converter.numEntered.value + " is
" + bitString);
}
function determineHexStartPower(num)
{
var power, multiple, quotient, firstDigit;
num = parseInt(validateInput(num));
alert("num = " + num);
power = Math.floor(Math.log(num)/Math.log(16));
multiple = Math.pow(16, power);
quotient = Math.floor(num / multiple);
firstDigit = determineHexDigit(quotient);
hexString = hexString + firstDigit.toString();
alert("power = " + power + "\nmultiple = " + multiple + "\nhexString = " +
hexString);
num = num - (multiple * quotient);
alert("num = " + num);
power--;
determineHexPlaceVal(num, power);
}
function determineHexPlaceVal(leftover, exponent)
{
var product, placeVal, hexDigit;
alert("leftover = " + leftover + "\nexponent = " + exponent + "\nproduct =
" + product);
for(; exponent >= 0; exponent--)
{
product = Math.pow(16, exponent);
alert("product = " + product + " after using the Math.pow fn");
if (leftover >= product)
{
placeVal = Math.floor(leftover / product);
alert("placeVal = " + placeVal + "\nleftover = " + leftover +
"\nproduct = " + product);
hexDigit = determineHexDigit(placeVal);
alert("hexDigit = " + hexDigit);
hexString = hexString + hexDigit.toString();
leftover = leftover - (product * placeVal);
alert("leftover = " + leftover + "\nexponent = " + exponent + "\nproduct = "
+ product +
"\nhexString = " + hexString);
}
else
{
hexString = hexString + "0";
alert("leftover = " + leftover + "\nexponent = " + exponent + "\nproduct = "
+ product +
"\nhexString = " + hexString);
}
}
document.write("The hexadecimal value of " + converter.numEntered.value +
" is " + hexString);
}
function determineHexDigit(num)
{
alert("num = " + num);
switch (num)
{
case 10:
return "A";
break;
case 11:
return "B";
break;
case 12:
return "C";
break;
case 13:
return "D";
break;
case 14:
return "E";
break;
case 15:
return "F";
break;
default:
return num;
}
}
</script>
</head>
<body>
<h1 align="center">Syster Tara's Number Converter</h1>
<h3>Please enter a positive integer into the text box and click either the
left button to get the
number's binary value or the right button to get its hexadecimal value</h3>
<form name="converter">
<table border="0" cellpadding="5" cellspacing="5">
<tr>
<td><a href="http://www.spacefem.com/blobs/"><img
src="http://www.maethos.info/~spacefem/pinkblob.gif" width="90" height="98"
border="0" alt="Adopt
your own useless blob!"></a></td>
<td><input type="text" name="numEntered"></td>
<td><input type="button" name="getBin" value="Get Binary Value"
onclick="determineBinStartPower(numEntered.value)"></td>
<td><input type="button" name="getHex" value="Get Hexadecimal Value"
onclick="determineHexStartPower(numEntered.value)"></td>
</tr>
</table>
</body>
</html>