/    Sign up×
Community /Pin to ProfileBookmark

login assistance

Hi im trying to create a form that allows a user to log in by a prompt box for a userid. I stored several arrays and if the user id does not match then there has to be an error message. If the user log-in matches then the next prompt box for the password comes up, and i want the same thing to happen with the password where i store arrays and it has to match, if it matches i use the document.write property for logging in successfully. i cant figure out what is wrong with my code, it works but it displays all 3 messages “welcome, invalid username and invalid password” no matter what i input. so the password prompt should not come up until the username is entered correctly with the stored arrays any help would be appreciated:
<body>
<script language=”JavaScript” type=”text/JavaScript”>
var check=””;
var match=”no”
var user_pass

var userid=new Array ();
login[0]=”batman”;
login[1]=”robin”;
login[2]=”spiderman”;

var use_name= prompt(“Please enter your usernaem”);

for (array_counter= 0; array_counter < 3; array_counter++)
{
if (use_name==login[array_counter])
{
var use_pass=prompt (“enter password”);
break;
} else {
continue;
}

match=”no”
}

if (match==”no”)
{
document.write (“Invalid username”);
}

var pass=new Array();
pass[3]=”pass1″;
passi[4]=”pass2″;
pass[5]=”pass3″;

for (array_counter= 3; array_counter < 5; array_counter++)
{
if (use_pass==pass[array_counter])
{
document.write (“logged in successfully!”);
break;
} else

document.write (“Invalid password”);
}

</script>
</body>
</html>

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@snowiekenFeb 15.2009 — Your script is just filled with errors. I'll try to give a quick overview:
[LIST]
  • [*]"match" is a js method and therefore a reserved word, better call your variable something else (I used "found")

  • [*]You set match to "no" outside of the if statement in your first loop, meaning it will be set to "no" every single time. That's why you always get "invalid username".

  • [*]You initialize your first array as "userid" and then assign values to "login[0]" (etc). Obviously those names need to be the same.

  • [*]You start assigning values to your second array at place 3, but it's an entirely new array than the previous one, so there's no need to. Assign values from the start of the array, which is 0.

  • [*]The reason you are getting both "invalid username" and "logged in succesfully": you check whether the username is correct, but when it isn't, you still execute the password code. When the username is wrong already, you don't want the password code to be executed. The solution is to put that code in the "else" statement.

  • [/LIST]


    <i>
    </i>&lt;script language="JavaScript" type="text/JavaScript"&gt;
    var found=0;

    var login=new Array();
    login[0]="batman";
    login[1]="robin";
    login[2]="spiderman";

    var use_name= prompt("Please enter your usernaem");

    for (array_counter= 0; array_counter &lt; login.length; array_counter++) {
    if (use_name==login[array_counter]) {
    var use_pass=prompt ("enter password");
    found=1;
    break;
    }
    else {
    continue;
    }
    }

    if (!found) {
    document.write ("Invalid username");
    }
    else {
    var pass=new Array();
    pass[0]="pass1";
    pass[1]="pass2";
    pass[2]="pass3";

    for (array_counter= 0; array_counter &lt; pass.length; array_counter++) {
    if (use_pass==pass[array_counter]) {
    document.write ("logged in successfully!");
    break;
    }
    else {
    document.write ("Invalid password");
    }
    }
    }



    &lt;/script&gt;

    I rewrote your script so that it works correctly. However, it's not a very good script for a login. The usernames and passwords need to match somehow, I presume? Because for now, if a user types any username from the array and any password from the other array, he is logged in...

    Javascript is also not the way to go when attempting a login. Any user can freely look into the source and log in to your page. Logins are better handled server side.
    Copy linkTweet thisAlerts:
    @freecycle004authorFeb 15.2009 — Thanks this actually helped alot, I didn't know for example when starting a new array to start over at "0" again, it works and it does what I wanted to do better
    ×

    Success!

    Help @freecycle004 spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 6.17,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @nearjob,
    tipped: article
    amount: 1000 SATS,

    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,

    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,
    )...