Need help with focus()

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>
 
V

Vjekoslav Begovic

Your focus() works just fine, but you didn't tell your script to stop
executing in case of invalid input. validateInput() should return true if
succeed, and false if don't. I've modified your code:

1.
function validateInput(userInput) // 8
{
if (isNaN(userInput)) // 10
{
alert("That's not a number. Try again."); // 12
converter.numEntered.focus(); // 13
return false; //validate failed
}
else if (userInput.indexOf(".") != -1) // 15
{
alert("That's not a whole number. Try again."); // 17
converter.numEntered.focus(); // 18
return false; //validate failed
}
else if (parseInt(userInput) < 0) // 20
{
alert("That's a negative number. Try a positive one"); // 22
converter.numEntered.focus(); // 23
return false;//validate failed
}
else
{
return true; //validate pass
}
}.

2.
function determineBinStartPower(num) // 38
{
var power, multiple;
if (!validateInput(num)) return;
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
}
 
D

Dr John Stockton

JRS: In article <[email protected]>, seen in
My code is included below. Your help
will be greatly appreciated.

Code for News should be written with 72-character margin, or otherwise
arranged so that the sending newsreader does not wrap it. Why should we
have to tediously reassemble the pieces when you could so easily have
done it yourself?

The whole method is far too long.

<URL:http://www.merlyn.demon.co.uk/js-maths.htm#Base> does a similar
task in three lines of script :

function BCvt() { with (document.forms['Frm1']) {
out.value = parseInt(inp.value, inpbase.value).
toString(outbase.value).toUpperCase() } }

That converts from any base in 2..36 to any base in 2..36.

You test your input number. Easier to test it with a RegExp :

OK = /^\d+$/.test(inp.value) // is it just 1 or more digits?

after which +inp.value gets the value, as a Number, with no possible
error (except too big, which you did not test).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,821
Latest member
AleidaSchi

Latest Threads

Top