How can I add more more cases for an IF statement in javascript

Viewed 54

So I have this code that is kind of like an authentication system but with JavaScript, and I know what you guys are gonna say that it's not secure and I know that, I know that I should use PHP, Ruby or other server-sided languages but this is just for fun. This is my code here:

function validateUser() {
  var username = document.getElementById("username").value;
  var password = document.getElementById("password").value;
  if (username == "username1" && password == "password1") {

    document.getElementById('submit').value = "Logging In...";
    setTimeout(function() {
      document.getElementById('wrapper').style.display = "none";
      document.getElementById('body').style.overflow = "auto";
      console.log('Welcome!');
    }, 1600);
  } else {
    document.getElementById('submit').value = "Logging In...";
    setTimeout(function() {
      document.getElementById('submit').value = "Login";
      var x = document.getElementById("snackbar");
      x.className = "show";
      setTimeout(function() {
        x.className = x.className.replace("show", "");
        console.error('Login Failed');
      }, 3000);
      document.getElementById('username').value = "";
      document.getElementById('password').value = "";
    }, 1300)
  }
  return false;
}

I tried putting a , like this if ( username == "username1", "username2"... but it just breaks the thing and even if I put the wrong value it will still log me in. I also tried to make another function with validateUser2() but then it shows the "Login Failed" but after 0.4 seconds then it disappears and shows the actual content. I also tried copying the if inside the same function but it causes the same error.

1 Answers

From the list of operators you are looking for the "logical or" operator || you can use it like:

// ...
if ( 
    ( username == "username1" && password == "password1" )
    || ( username == "username2" && password == "password2" )
){
// ...
Related