I cannot get my focus() fn to work in my validateInput(u serInput) 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="JavaS cript">
var bitString="";
var hexString=""; // 6
function validateInput(u serInput) // 8
{
if (isNaN(userInpu t)) // 10
{
alert("That's not a number. Try again."); // 12
converter.numEn tered.focus(); // 13
}
else if (userInput.inde xOf(".") != -1) // 15
{
alert("That's not a whole number. Try again."); // 17
converter.numEn tered.focus(); // 18
}
else if (parseInt(userI nput) < 0) // 20
{
alert("That's a negative number. Try a positive one"); // 22
converter.numEn tered.focus(); // 23
}
else
{
return userInput;
}
}
function determineBinSta rtPower(num) // 38
{
var power, multiple;
num = validateInput(n um);
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 + "\nbitStrin g = " +
bitString);
num = num - multiple; // 47
alert("num = " + num); // 48
power--; // 49
determineBinBit Val(num, power); // 51
}
function determineBinBit Val(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 +
"\nbitStrin g = " + bitString); // 66
}
else
{
bitString = bitString + "0"; // 70
alert("leftover = " + leftover + "\nexponent = " + exponent + "\nproduct = "
+ product +
"\nbitStrin g = " + bitString);
}
}
document.write( "The binary value of " + converter.numEn tered.value + " is
" + bitString);
}
function determineHexSta rtPower(num)
{
var power, multiple, quotient, firstDigit;
num = parseInt(valida teInput(num));
alert("num = " + num);
power = Math.floor(Math .log(num)/Math.log(16));
multiple = Math.pow(16, power);
quotient = Math.floor(num / multiple);
firstDigit = determineHexDig it(quotient);
hexString = hexString + firstDigit.toSt ring();
alert("power = " + power + "\nmultiple = " + multiple + "\nhexStrin g = " +
hexString);
num = num - (multiple * quotient);
alert("num = " + num);
power--;
determineHexPla ceVal(num, power);
}
function determineHexPla ceVal(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(left over / product);
alert("placeVal = " + placeVal + "\nleftover = " + leftover +
"\nproduct = " + product);
hexDigit = determineHexDig it(placeVal);
alert("hexDigit = " + hexDigit);
hexString = hexString + hexDigit.toStri ng();
leftover = leftover - (product * placeVal);
alert("leftover = " + leftover + "\nexponent = " + exponent + "\nproduct = "
+ product +
"\nhexStrin g = " + hexString);
}
else
{
hexString = hexString + "0";
alert("leftover = " + leftover + "\nexponent = " + exponent + "\nproduct = "
+ product +
"\nhexStrin g = " + hexString);
}
}
document.write( "The hexadecimal value of " + converter.numEn tered.value +
" is " + hexString);
}
function determineHexDig it(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.co m/blobs/"><img
src="http://www.maethos.inf o/~spacefem/pinkblob.gif" width="90" height="98"
border="0" alt="Adopt
your own useless blob!"></a></td>
<td><input type="text" name="numEntere d"></td>
<td><input type="button" name="getBin" value="Get Binary Value"
onclick="determ ineBinStartPowe r(numEntered.va lue)"></td>
<td><input type="button" name="getHex" value="Get Hexadecimal Value"
onclick="determ ineHexStartPowe r(numEntered.va lue)"></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="JavaS cript">
var bitString="";
var hexString=""; // 6
function validateInput(u serInput) // 8
{
if (isNaN(userInpu t)) // 10
{
alert("That's not a number. Try again."); // 12
converter.numEn tered.focus(); // 13
}
else if (userInput.inde xOf(".") != -1) // 15
{
alert("That's not a whole number. Try again."); // 17
converter.numEn tered.focus(); // 18
}
else if (parseInt(userI nput) < 0) // 20
{
alert("That's a negative number. Try a positive one"); // 22
converter.numEn tered.focus(); // 23
}
else
{
return userInput;
}
}
function determineBinSta rtPower(num) // 38
{
var power, multiple;
num = validateInput(n um);
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 + "\nbitStrin g = " +
bitString);
num = num - multiple; // 47
alert("num = " + num); // 48
power--; // 49
determineBinBit Val(num, power); // 51
}
function determineBinBit Val(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 +
"\nbitStrin g = " + bitString); // 66
}
else
{
bitString = bitString + "0"; // 70
alert("leftover = " + leftover + "\nexponent = " + exponent + "\nproduct = "
+ product +
"\nbitStrin g = " + bitString);
}
}
document.write( "The binary value of " + converter.numEn tered.value + " is
" + bitString);
}
function determineHexSta rtPower(num)
{
var power, multiple, quotient, firstDigit;
num = parseInt(valida teInput(num));
alert("num = " + num);
power = Math.floor(Math .log(num)/Math.log(16));
multiple = Math.pow(16, power);
quotient = Math.floor(num / multiple);
firstDigit = determineHexDig it(quotient);
hexString = hexString + firstDigit.toSt ring();
alert("power = " + power + "\nmultiple = " + multiple + "\nhexStrin g = " +
hexString);
num = num - (multiple * quotient);
alert("num = " + num);
power--;
determineHexPla ceVal(num, power);
}
function determineHexPla ceVal(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(left over / product);
alert("placeVal = " + placeVal + "\nleftover = " + leftover +
"\nproduct = " + product);
hexDigit = determineHexDig it(placeVal);
alert("hexDigit = " + hexDigit);
hexString = hexString + hexDigit.toStri ng();
leftover = leftover - (product * placeVal);
alert("leftover = " + leftover + "\nexponent = " + exponent + "\nproduct = "
+ product +
"\nhexStrin g = " + hexString);
}
else
{
hexString = hexString + "0";
alert("leftover = " + leftover + "\nexponent = " + exponent + "\nproduct = "
+ product +
"\nhexStrin g = " + hexString);
}
}
document.write( "The hexadecimal value of " + converter.numEn tered.value +
" is " + hexString);
}
function determineHexDig it(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.co m/blobs/"><img
src="http://www.maethos.inf o/~spacefem/pinkblob.gif" width="90" height="98"
border="0" alt="Adopt
your own useless blob!"></a></td>
<td><input type="text" name="numEntere d"></td>
<td><input type="button" name="getBin" value="Get Binary Value"
onclick="determ ineBinStartPowe r(numEntered.va lue)"></td>
<td><input type="button" name="getHex" value="Get Hexadecimal Value"
onclick="determ ineHexStartPowe r(numEntered.va lue)"></td>
</tr>
</table>
</body>
</html>
Comment