Old 20-06-2007   #1 (permalink)
Banned
 
Join Date: Feb 2007
Location: Sydney
Age: 19
Posts: 4,205
Rep Power: 0
Default JS Login Help!!

This seems the right place to ask :P

I'm making a JS login using an array, which locks the user out after 3 tries. It's for an assignment, and I wouldn't usually ask but for the life of me, I can't figure out why it still says "x incorrect login attempts" even when I get it right.

Code:
var username = new Array();
username[0] = "Blake";
username[1] = "Morgan";
username[2] = "Steve";
username[3] = "Admin";
username[4] = "Adam";

var password = new Array();
password[0] = "pass1";
password[1] = "pass2";
password[2] = "pass3";
password[3] = "pass4";
password[4] = "pass5";

var logincheck = false;
var Number = 1;

function Login()

   {

	for (var i = 0; i < username.length; i = i + 1)
		{
		if ((username[i] == document.LForm.UName.value) && (password[i] == document.LForm.PWord.value))
			{
			window.location.href="members.htm";
			logincheck = true;
			}	
	    	}
		 
	{
		if (!logincheck)
		{ 	
		alert("Incorrect Username or Password combination.");
		} 
	{
  	do {
   	    alert(Number + " " + "incorrect login attempts.");
    	    Number++;
  		}
		while(Number < 1)
  		}
	}
	if (Number > 3)
		{
		document.location.href = 'blocked.htm'
		}
    }
TIA, i'm going insane over this.. lol!
Blake is offline   Reply With Quote
Old 20-06-2007   #2 (permalink)
Monster Member
 
FuRbY*'s Avatar
 
Join Date: Dec 2006
Location: the immunity syndicate
Age: 30
Posts: 2,998
Rep Power: 5
Default

not that im familiar with JS, but whats the benefit in using an array over an if statement?
FuRbY* is offline   Reply With Quote
Old 20-06-2007   #3 (permalink)
Banned
 
Join Date: Feb 2007
Location: Sydney
Age: 19
Posts: 4,205
Rep Power: 0
Default

The benefit is my assignment indicates I need to use an array

Else i'd have finished ages ago :P
Blake is offline   Reply With Quote
Old 20-06-2007   #4 (permalink)
Pro Member
 
chemicalNova's Avatar
 
Join Date: Jun 2006
Age: 19
Posts: 5,593
Rep Power: 8
Default

You have a few stray brackets all over the place..
Code:
var username = new Array();
username[0] = "Blake";
username[1] = "Morgan";
username[2] = "Steve";
username[3] = "Admin";
username[4] = "Adam";

var password = new Array();
password[0] = "pass1";
password[1] = "pass2";
password[2] = "pass3";
password[3] = "pass4";
password[4] = "pass5";

var logincheck = false;
var Number = 1;

function Login()

   {

	for (var i = 0; i < username.length; i = i + 1)
		{
		if ((username[i] == document.LForm.UName.value) && (password[i] == document.LForm.PWord.value))
			{
			window.location.href="members.htm";
			logincheck = true;
			}	
	    	}
		 
	{ <---- that one
		if (!logincheck)
		{ 	
		alert("Incorrect Username or Password combination.");
		} 
	{ <--- that one
  	do {
   	    alert(Number + " " + "incorrect login attempts.");
    	    Number++;
  		}
		while(Number < 1)
  		} <------
	} <---- this one and the above one
	if (Number > 3)
		{
		document.location.href = 'blocked.htm'
		}
    }
It throws your code completely out of whack. Try removing the ones I suggested.. You're also missing a semicolon after the while.

What browser are you using? I'm fairly certain IE would throw errors with that script.

chem
__________________
There are no stupid questions... but there are alot of inquisitive idiots.
-
chemicalNova is offline   Reply With Quote
Old 20-06-2007   #5 (permalink)
Banned
 
Join Date: Feb 2007
Location: Sydney
Age: 19
Posts: 4,205
Rep Power: 0
Default

Using both IE and Firefox, as I have to comply to w3c standards.

And yeah, i'm a fairly messy coder, lol.
Blake is offline   Reply With Quote
Old 20-06-2007   #6 (permalink)
Pro Member
 
chemicalNova's Avatar
 
Join Date: Jun 2006
Age: 19
Posts: 5,593
Rep Power: 8
Default

Did that fix it? Having the brackets there stuff up the parsing completely.

(btw, i=i+1 looks so yuck.. try ++i or i++ )

chem
__________________
There are no stupid questions... but there are alot of inquisitive idiots.
-
chemicalNova is offline   Reply With Quote
Old 20-06-2007   #7 (permalink)
Banned
 
Join Date: Feb 2007
Location: Sydney
Age: 19
Posts: 4,205
Rep Power: 0
Default

Same thing though! And nah, neither change anything
Blake is offline   Reply With Quote
Old 20-06-2007   #8 (permalink)
Pro Member
 
chemicalNova's Avatar
 
Join Date: Jun 2006
Age: 19
Posts: 5,593
Rep Power: 8
Default

Can't read **** on these forums so I re-wrote it. On the third attempt it goes to blocked ..
Code:
var username = new Array();
username[0] = "Blake";
username[1] = "Morgan";
username[2] = "Steve";
username[3] = "Admin";
username[4] = "Adam";

var password = new Array();
password[0] = "pass1";
password[1] = "pass2";
password[2] = "pass3";
password[3] = "pass4";
password[4] = "pass5";

var wrong_attempts = 0;
var success = false;

function Login()
{
    for (var i = 0; i < username.length; ++i)
    {
        if (wrong_attempts > 1)
            window.location.href = "blocked.htm";

        if ((username[i] == document.LForm.UName.value) && (password[i] == document.LForm.PWord.value)) {
            success = true;
            window.location.href = "members.htm";
            break;        
        }
    }

    if (!success) {
        alert('Incorrect Username or Password combination');
        wrong_attempts++;
    }
}
chem
__________________
There are no stupid questions... but there are alot of inquisitive idiots.
-

Last edited by chemicalNova; 20-06-2007 at 04:35 PM.. Reason: ****ed the loop LOL
chemicalNova is offline   Reply With Quote
Old 20-06-2007   #9 (permalink)
Pro Member
 
chemicalNova's Avatar
 
Join Date: Jun 2006
Age: 19
Posts: 5,593
Rep Power: 8
Default

I fixed the above post.. I ****ed the loop. Should work now.

chem
__________________
There are no stupid questions... but there are alot of inquisitive idiots.
-
chemicalNova is offline   Reply With Quote
Old 20-06-2007   #10 (permalink)
Banned
 
Join Date: Feb 2007
Location: Sydney
Age: 19
Posts: 4,205
Rep Power: 0
Default

Thanks heaps
Blake is offline   Reply With Quote
Old 21-06-2007   #11 (permalink)
Contributing Member
 
Join Date: Jun 2006
Posts: 607
Rep Power: 3
Default

Quote:
Originally Posted by Blake View Post
Using both IE and Firefox, as I have to comply to w3c standards.

And yeah, i'm a fairly messy coder, lol.
You realise conforming to w3c standards does not make your code work in both browsers.

Case in point, if we want to set an event callback, setAttribute() the standard versus
attachEvent() or obj.property = function() { ... } for ie.
__________________
92% of teens have moved on to rap. If you are part of the 8% who still listen to real music, copy and paste this into your signature.
Khaless is offline   Reply With Quote
Old 21-06-2007   #12 (permalink)
Contributing Member
 
Join Date: Jun 2006
Posts: 607
Rep Power: 3
Default

Remember JS authentication is broken by definition ('client side'). And also, if you ever feel like making things neater, look at how we can form objects on the fly in js. For Example

var details = [ {username: 'jim', password: 'jimmy'}, {username: 'bob', password: 'bobby'} ]

Will give us basically an array of username/password tuples (objects)
so we could
details[0].password // jim's password

which means you could:

for (var i in details) {
if(details[i].username == ...
}

gl, hf!
__________________
92% of teens have moved on to rap. If you are part of the 8% who still listen to real music, copy and paste this into your signature.
Khaless is offline   Reply With Quote
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT +10. The time now is 10:36 AM.


Powered by vBulletin® Version 3.7.5
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0