Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java and JDK > Java Basics
|
Java Basics General beginning Java language questions that don't fit in one of the more specific forums. Please specify what version.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Java Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 23rd, 2005, 03:41 PM
Registered User
 
Join Date: Dec 2005
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default login script: user can't hit "return" for login

Hi, I am very new to js and am using a simple login script I found at www.laughland.biz. I needed this script to do 2 things it did't:

setfocus on the username field

allow the usesr to hit return after entering their pw and be logged in.

I got the first one, but can't for the life of me get the second.
Here's the code block I'm assuming this function would be in:

function BuildPanel() {
document.write('<form name="logon"><table align="center" border="0"><tr><td align="center">');
document.write('Username:</td>');
document.write('<td><input type="text" name="username" size="20"></td></tr>');
document.write('<tr><td align="right">Password:</td>');
document.write('<td><input type="password" name="password" size="20"></td></tr>');
if(imgSubmit == ""){
 document.write('<tr><td align="center" colspan="2"><p><input type="button" name="Logon" value="Logon" onClick="login(username.value,password.value)" onKeyPress="login(username.value,password.value)"> ');
} else {
 document.write('<tr><td align="center" colspan="2"><p><input type="image" src="'+imgSubmit+'" name="Logon" onclick="login(username.value,password.value)">');
}
if(imgReset == ""){
 document.write('<input type="reset" value="Reset" name="Reset">');
} else {
 document.write('<input type="image" src="'+imgReset+'" name="Reset" onclick="logon.reset();">');
}
document.write('</p></td></tr></table></form>');
}

function setfocus() {
    document.logon.username.focus();
}

K. Dawn Merrill
 
Old December 24th, 2005, 02:49 PM
Authorized User
 
Join Date: Dec 2005
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Dawn,

Try doing this in your javascript:

if(imgSubmit == ""){
 document.write('<tr><td align="center" colspan="2"><p><input type="submit" name="Logon" value="Logon" onClick="login(username.value,password.value)" onKeyPress="login(username.value,password.value)"> ');
}

Note the type of the input. A submit button will automatically get fired when you
hit the enter button.

________
Adarsh
 
Old December 29th, 2005, 11:46 AM
Registered User
 
Join Date: Dec 2005
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

HI Adarsh, and thanks for the information. I tried this & while it DOES allow the user to simply hit enter, it just returns to the same page & resets it to blank fields instead of going on to the next page. What am I doing wrong here? The only value I changed was the input type="submit"

Thanks, Dawn

K. Dawn Merrill
 
Old January 16th, 2006, 05:21 AM
Friend of Wrox
 
Join Date: Jan 2006
Posts: 198
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Dawn,

Now that it's a submit button, when the user presses enter the browser goes to the "action" URL defined in your <form> element. To prevent this from happening (and to just run the login() javascript), change the following code

onClick="login(username.value,password.value)"

to this:

onClick="login(username.value,password.value); return false"

This should abort the default submit action and let login() set the window location to the URL you're expecting.

Let me know how it works!

Jon Emerson
Computer Scientist
Adobe Systems, Inc.
http://www.jonemerson.net/
 
Old January 16th, 2006, 05:55 PM
Registered User
 
Join Date: Dec 2005
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Jon, that's perfect! Thanks so much. Now I have one more question: I lose my setfocus if the user hits reset. Any ideas?

Thanks again, Dawn

K. Dawn Merrill
 
Old January 16th, 2006, 07:07 PM
Friend of Wrox
 
Join Date: Jan 2006
Posts: 198
Thanks: 0
Thanked 0 Times in 0 Posts
Default

What form element do you want focused after reset is clicked?

Jon Emerson
Adobe Systems, Inc.
http://www.jonemerson.net/
 
Old January 17th, 2006, 10:40 AM
Registered User
 
Join Date: Dec 2005
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I would like it to go back to "username" after reset is clicked. Thanks for your help!

Dawn

K. Dawn Merrill
 
Old January 17th, 2006, 12:15 PM
Friend of Wrox
 
Join Date: Jan 2006
Posts: 198
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Dawn,

I would do something like this:

Code:
<html>
<body>
<form action="test.html" method="get" name="logon">
<input type="text" name="username">
<input type="submit">
<input type="image" src="http://media.wiley.com/product_data/coverImage80/28/07645590/0764559028.jpg" 
  onclick="document.logon.reset(); document.logon.username.focus(); return false">
</form>
</body>
</html>
You might want to move the onclick() code into its own function -- but this shows the basic idea.

Jon Emerson
Adobe Systems, Inc.
http://www.jonemerson.net/
 
Old January 17th, 2006, 06:27 PM
Registered User
 
Join Date: Dec 2005
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Jon, I'll give that a try. Today is Friday for me, but I'll probably try it from home over my "weekend". I'll let you know how it works!

Thanks Again, Dawn

K. Dawn Merrill
 
Old July 12th, 2006, 04:28 AM
rkn rkn is offline
Registered User
 
Join Date: Jul 2006
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi
I'm using the exact same login script, but I am new to this and I can't get make the setfocus work.

Besides the
function setfocus() {
    document.logon.username.focus();
}

do I need to add anything to the html, etc.?









Similar Threads
Thread Thread Starter Forum Replies Last Post
login script jstewie Javascript How-To 1 July 13th, 2005 12:52 PM
Newbie Help. Login to unique login page per user Kainan Classic ASP Professional 10 May 3rd, 2005 07:47 AM
login failed for user nt authority\anonymous login rj1406 Classic ASP Databases 1 October 24th, 2004 09:15 AM
Login Script natmaster PHP How-To 10 June 19th, 2003 03:50 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.