samedi 24 avril 2021

if/else not working in Javascript Login Page

I have created a Login Page with HTML, CSS & JS. If the value in <input> is correct, the js code takes me to the location I want with the code window.location.replace("href") and if the value is incorrect, it displays an alert. But I want a code so that if the input is empty it would show another alert not the previous one. I have also used required field in html <input> tag: <input type="password" id="password" class="input" required>, but by using required the whole code doesn't works. The code is:

Javascript

function auth(event) {
      event.preventDefault();

      var username = document.getElementById("username").value;
      var password = document.getElementById("password").value;

      if (username === "admin@gmail.com" && password === "user") {
           window.location.replace("http://www.rex14.ml/");
      } else {
          alert("Please enter valid information");
          return;
      }
}

I have tried this but it is not working: `

    function auth(event) {
          event.preventDefault();

          var username = document.getElementById("username").value;
          var password = document.getElementById("password").value;

          if (username === "admin@gmail.com" && password === "user") {
               window.location.replace("http://www.rex14.ml/");
          } else {
              alert("Please enter valid information");
              return;
          }
 if (username === "" && password === "") {
               alert("Please enter information");
          } else {
              
              return;
          }
    }

`

Aucun commentaire:

Enregistrer un commentaire