473,388 Members | 915 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

Need help with focus()

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>
Jul 20 '05 #1
2 4534
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
}
"TSK" <su******@mindspring.com> wrote in message
news:bd**********@slb6.atl.mindspring.net...
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>

Jul 20 '05 #2
JRS: In article <bd**********@slb6.atl.mindspring.net>, seen in
news:comp.lang.javascript, TSK <su******@mindspring.com> posted at Fri,
27 Jun 2003 10:36:54 :-
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).

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MSIE 4
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> JS maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.
Jul 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: George Hester | last post by:
In a page I have when the user left-clicks the page a Input box for a form gets the focus. But if the user right-clicks the page the Input box is not getting the focus. I'd like the Input box to...
2
by: Czarina | last post by:
hi guys! here I am again, bugging you Here is where my page stands right now: http://www.gainesvillewebs.com/czar...h_results-2.htm The top 2 forms are working just fine, but the bottom one, with...
9
by: CW | last post by:
I wrote an HTML based chat application. The front end is built entirely on HTML + javascript. Essentially, I have a hidden frame that's refreshed frequently and any new messages are displayed in...
11
by: Junkguy | last post by:
I need some help programmatically causing a row in a DataGrid to "flush" its contents to its bound data (in Visual Studio 6 using Windows Forms with C#). My issue is I want to send an update to...
18
by: jrhoads23 | last post by:
Hello, I am trying to find a way to tell if an .NET windows forms Button (System.Windows.Forms.Button) is "depressed" (pushed down). For my application, I can not use a check box control set to...
1
by: Bill Borg | last post by:
Hello, I would like to skip over a hyperlink in the tab order. The link is not that important, and I don't mind that they have to click to get it. I have tried OnFocus="this.blur();" on the...
18
by: Euphor2 | last post by:
I'm in school, taking a VB .NET 2003 course, and the teacher taught us how to use Sender.Focus() to return focus to the control from which focus was received. However, when I got home, and tried...
46
by: Bruce W. Darby | last post by:
This will be my very first VB.Net application and it's pretty simple. But I've got a snag in my syntax somewhere. Was hoping that someone could point me in the right direction. The history: My...
8
by: Brad Isaacs | last post by:
Good morning friends, I am working with Visual Studio 2005, ASP.NET 2.0 I am working with the Login controls provided my .NET 2.0, trying to make the Login1 control UserName textbox obtain...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.