0

My window.location.replace is not working, by which I mean it's not redirecting. Please help.

My website:

enter image description here

function validate(){
    var username=document.getElementById('login-username').value;
    var passowrd=document.getElementById('login-password').value;

    if (username === "daksh" && passowrd === 'daksh'){
        alert('You have sucessfully logged in');
        window.location.replace("http://stackoverflow.com");
    } else{
        alert('Wrong username or password');
        return true;
    }
}
<div action="login.html" class="container">
 <form  method="POST" class="login-form">
    <h1 class="login-heading">LOGIN FORM</h1>
     <input type="text" placeholder="User Name" id="login-username">
     <br><br>
     <input type="password" placeholder="Password" id="login-password">
     <br><br><br>
     <input type="submit" value="Login" id="login-submit" onclick="validate()">
 </form>
</div>
Nat Riddle
  • 928
  • 1
  • 10
  • 24
  • 1
    [Duplicate](//google.com/search?q=site%3Astackoverflow.com+js+prevent+form+submission) of [How to prevent form from being submitted?](/q/3350247/4642212). You need to prevent form submission if you need validation or custom behavior. Use `yourForm.addEventListener("submit", (event) => { event.preventDefault(); /* Perform validation. When ready to submit: */ event.target.submit(); });`. – Sebastian Simon Sep 10 '21 at 13:47
  • Why does your `
    ` have an `action="login.html"`? Why use a `
    ` in the first place if you don’t use any of its benefits? Please [validate your HTML](//html5.validator.nu/).
    – Sebastian Simon Sep 10 '21 at 13:50
  • Inline event handlers like `onclick` are [not recommended](/q/11737873/4642212). They are an [obsolete, hard-to-maintain and unintuitive](/a/43459991/4642212) way of registering events. Always [use `addEventListener`](//developer.mozilla.org/docs/Learn/JavaScript/Building_blocks/Events#inline_event_handlers_%E2%80%94_dont_use_these) instead. – Sebastian Simon Sep 10 '21 at 13:53
  • Hey, just wanted to point out that the link there is not formatted properly. Please do remember to always check the preview of your post. Goodluck :D – I_love_vegetables Sep 16 '21 at 10:00

0 Answers0