0

OK so I am making a register and login for a forum using javascript and localstorage. "School assignment" My problem is that when i created multiple user and store them in the localstorage, my for loop does not loop through them all, only the first one. So i can only access the forum with the first user i create.

function login ()  {
    if (checklogin()) {
        boxAlert.style.display = "block";
        boxAlert.innerHTML = "Welcome" + "";
        wallPanel.style.display = "block";
    } else {
        boxAlertfail.style.display = "block";
        boxAlertfail.innerHTML = "Go away, fail";
    }
}

function checklogin (){
    for  (var i = 0; i < aUsers.length; i++){
        if (aUsers[i].email == inputLoginMail.value && aUsers[i].password == inputLoginPassword.value){
            return true;
        }else{
            return false;
        }
    }
}
3
  • put in a console.log(aUsers) before you loop through them in checklogin Commented Sep 24, 2014 at 14:49
  • 4
    you're returning false after the first user fails your if so the for loop and function stop right there Commented Sep 24, 2014 at 14:49
  • aUsers is empty. var aUsers = []; Commented Sep 24, 2014 at 14:53

2 Answers 2

1

how about:

function checklogin() {
    var validLogin = false;
    for (var i = 0; i < aUsers.length; i++) {
        if (aUsers[i].email == inputLoginMail.value 
            && aUsers[i].password == inputLoginPassword.value) {
            validLogin = true;
            break;
        }
    }
    return validLogin;
}
Sign up to request clarification or add additional context in comments.

Comments

0

Ouch! You are returning false on very first attempt. The best way is to set a variable and then check for it.

function checklogin() {
        var z = 0;
        for (var i = 0; i < aUsers.length; i++) {
            if (aUsers[i].email == inputLoginMail.value && aUsers[i].password == inputLoginPassword.value) {
                z = 1;
                break;
            } else {
                z = 0;
            }

            if (z == 1) {
                // User logged in
            } else {
                // Fake user
            }
        }

1 Comment

Dosn't retrieve anything from the local storage when i try to login

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.