homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

Login with redirect after 3 failed attempts

 3:30 am on Apr 26, 2006 (gmt 0)

I am stuck on an assignment with the following piece of code.

Basically we need to be able to code a login to a protected area using an array of usernames and passwords.
When the user enters the correct username and password they gain entry to the "members" page.
This bit I have got working OK.

If they enter an incorrect name and password they get three tries and then redirect to a failure page.
This is where I am stuck. When the username and password entered into the form and the submit button clicked nothing happens but when the submit button is clicked when the form is empty the alert pops up.

Also any suggestions on the loop would be great as I have tried about a million things with no luck and the teacher has been less than helpful on this.


function Login()


var username=document.login.username.value;
var password=document.login.password.value;

var TheUsers = new Array(5)
TheUsers[0] = "username1:password1";
TheUsers[1] = "username2:password2";
TheUsers[2] = "username3:password3";
TheUsers[3] = "username4:password4";
TheUsers[4] = "username5:password5";
for (i = 0; i < TheUsers.length; i++)
if (TheUsers[i].indexOf(username) == 0)
var TheSplit = TheUsers[i].split(":");

var TheUsername = TheSplit[0];
var ThePassword = TheSplit[1];

if (username == TheUsername && password == ThePassword)

alert("Incorrect Username or Password");







 7:09 am on Apr 26, 2006 (gmt 0)

is this in javascript? I mean you use array for id/password in javascript? it will generate big problem for your system. any1 may figure out the password/id easily.


 7:52 am on Apr 26, 2006 (gmt 0)

Stupid I know. Why we have to do it is beyond me. It is only for an assignment and the site will never go live.

I guess it is to help us understand the concept as we are doing PHP next.

Anway do you have any suggestions.


 4:54 pm on Apr 26, 2006 (gmt 0)

Here are my suggestions:

Keep the username and password separated in your array instead of combining them in a single string. The reason is that if someone enters a username that contains your delimiter (:) then your logic will get screwed up. You can have an array of arrays like this:

var TheUsers = new Array(5)
TheUsers[0] = new Array("username1","password1");
TheUsers[1] = new Array("username2","password2");
TheUsers[2] = new Array("username3","password3");
TheUsers[3] = new Array("username4","password4");
TheUsers[4] = new Array("username5","password5");

You could then access the username like this (this is just to give you an idea of how to access the information above, in case you don't understand it):

alert(TheUsers[0][0]); // alerts "username1"
alert(TheUsers[4][0]); // alerts "username5"

And the passwords:

alert(TheUsers[0][1]); // alerts "password1"
alert(TheUsers[4][1]); // alerts "password5"

Next, your loop. If a match is found, there's no need to continue processing the loop. Therefore, once you open the window, you can return and you're done. However, if that never happens, then we can assume that either the username wasn't found, or a username was found but passwords didn't match:

for (i = 0; i < TheUsers.length; i++)
if (TheUsers[i][0] == username)
// Compare the passwords
if (TheUsers[i][1] == password)
return; // Stop processing, we're done
// If we're still here, then no match was found
alert("Incorrect Username or Password");

Hope that helps.


 4:58 pm on Apr 26, 2006 (gmt 0)

I should also mention that iterating through an array like this is not an efficient way to do username/password comparisons, even with PHP. But I'll assume it's just part of the project and helping you to learn arrays.

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved