Check login with arrays

Asked

Viewed 27 times

0

Hello! I am trying to make a code that allows checking if a given email and password are within certain arrays, so far this code works, however I can’t make the code detect errors like: if the email is in the array but does not match the password, thus releasing an Alert.

function Entrada_login() {
            var password = document.getElementById("Password").value;
            var email = document.getElementById("Email").value;
            for (var i = 0; i < emails.length; i++) {
                if ((email == emails[i]) && (password == passwords[i])) {
                    i = i;
                    alert("funciona");

                    break;
                }
            }
        } 

Thank you!!

  • Could you ask the question array who makes the comparisons!

1 answer

0


You can’t just add one else if? For example:

function Entrada_login() {
    var password = document.getElementById("Password").value;
    var email = document.getElementById("Email").value;
    for (var i = 0; i < emails.length; i++) {
        if ((email == emails[i]) && (password == passwords[i])) {
            i = i;
            alert("funciona");

            break;
        } else if ( (email == emails[i]) ) {
            i = i;
            alert("achou o email mas a senha nao confere");

            break;
        }
    }
} 
  • I tried, but from that moment on, it starts to give error signal in the div where it calls to that code. So I was wondering if it’s the fact that it’s inside a loop or if it has to do with something else.

  • 1

    @Strawberry20 good code works https://jsfiddle.net/k7oujLma/ , may be something else, what error returns?

  • 1

    I guess my code didn’t work after all because in the process I must have deleted a } and that gave me Unexpected Error, but now your code has made it work. Thank you very much!! I have to start paying more attention to these details.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.