Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

162
Visualizações
JavaScript if-else condition always returns false

I am making a login page as my assignment using only HTML, CSS and JavaScript. The problem is that even after entering the correct id and password, condition returns false.

This is my code:

var loginForm = document.getElementById("login-form");
loginErrorMsg = document.getElementById("login-error-msg");
var UN = loginForm.username.value;
var PS = loginForm.password.value;
function LogIn() {
    if (UN === "user" && PS === "pass") {
        // location.reload();
        console.log("You have successfully logged in.");
    } else {
        console.log("Wrong password");
    }
}
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Login</title>
    <link rel="stylesheet" href="Login.css">
    <script defer src="Login.js"></script>
</head>

<body>
    <video playsinline autoplay muted loop poster="Background_Snap.png" id="bgvid">
        <source src="Background_Video.mp4" type="video/mp4">
    </video>
    <main id="main-holder">
        <h1 id="heading">Login</h1>

        <div id="login-error">
            <p id="login-error-msg">Invalid username <span id="login-error-msg-2">and/or password</span></p>
        </div>

        <form id="login-form" onsubmit="return false;">
            <input type="text" name="username" id="username-field" class="login-form-input" placeholder="Username"
                required>
            <input type="password" name="password" id="password-field" class="login-form-input" placeholder="Password"
                required>
            <button type="submit" onclick="LogIn()" id="login-submit">LogIn</button>
        </form>

    </main>
</body>

</html>

Please look into this and tell me where I am doing wrong. I have tried everything I could thing of.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

After making a snippet, we easier find the issue.

Firstly, you call multiple times the login form:

  • On the onsubmit in the <form>
  • With the <input> when clicking. In the snippet, I removed one.

Then, you load values only one time. It's confusing, only about "when the LogIn method is called". So, when we include everything in the method, it's fine.

Why put everything in it ? Because div can change, be removed or created. It's to prevent to use wrong values.

function LogIn() {
    var loginForm = document.getElementById("login-form");
    loginErrorMsg = document.getElementById("login-error-msg");
    var UN = loginForm.username.value;
    var PS = loginForm.password.value;
    if (UN === "user" && PS === "pass") {
        // location.reload();
        console.log("You have successfully logged in.");
    } else {
        console.log("Wrong password");
    }
}
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Login</title>
    <link rel="stylesheet" href="Login.css">
    <script defer src="Login.js"></script>
</head>

<body>
    <video playsinline autoplay muted loop poster="Background_Snap.png" id="bgvid">
        <source src="Background_Video.mp4" type="video/mp4">
    </video>
    <main id="main-holder">
        <h1 id="heading">Login</h1>

        <div id="login-error">
            <p id="login-error-msg">Invalid username <span id="login-error-msg-2">and/or password</span></p>
        </div>

        <form id="login-form" onsubmit="return false;">
            <input type="text" name="username" id="username-field" class="login-form-input" placeholder="Username"
                required>
            <input type="password" name="password" id="password-field" class="login-form-input" placeholder="Password"
                required>
            <button type="submit" onclick="LogIn()" id="login-submit">LogIn</button>
        </form>

    </main>
</body>

</html>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda