admin管理员组

文章数量:1323529

I need to have a login form that validates and after login the page needs to redirect to a html page. I also need to hard code in a username and password so only with the correct user name/password the page will redirect. If it doesn't have the correct user/pass then I need an alert.

I've tried a bunch of variations but I know I missing something or using the code wrong. Please help! :) Thanks! This is my code so far.

JS:

function validateForm() {
    'use strict';

    // Get references to the form elements:
    var email = document.getElementById('email');
    var password = document.getElementById('password');

    // Validate the login
    if ((email.value.length > 0) && (password.value.length > 0)) {
        return true;
    } else {
        alert('Please plete the form!');
        return false;
    }
}

function check(form) {

    var emailArray = ("[email protected]", "");
    var passwordArray = ("MyLogIn", "");

    if (email.value == "email" && password.value == "password") {
        window.open('myaccount.html');
    } else {
        alert('Please enter correct username or password!');
        return false;
    }   
}

function init() {
    'use strict';

    // Confirm that document.getElementById() can be used:
    if (document && document.getElementById) {
        var loginForm = document.getElementById('lgform');
        loginForm.onsubmit = validateForm;
    }
}
window.onload = init;

HTML:

<form action="" method="post" id="lgform">
    <fieldset>
        <legend>Login</legend>
        <div>
            <label for="email">Email Address</label>
            <input type="email" name="email" id="email" required>
        </div>
        <div>
            <label for="password">Password</label>
            <input type="password" name="password" id="password" required>
        </div>
        <div>
            <label for="submit"></label>
            <input type="submit" value="Login" id="submit">
        </div>
    </fieldset>
</form>

I need to have a login form that validates and after login the page needs to redirect to a html page. I also need to hard code in a username and password so only with the correct user name/password the page will redirect. If it doesn't have the correct user/pass then I need an alert.

I've tried a bunch of variations but I know I missing something or using the code wrong. Please help! :) Thanks! This is my code so far.

JS:

function validateForm() {
    'use strict';

    // Get references to the form elements:
    var email = document.getElementById('email');
    var password = document.getElementById('password');

    // Validate the login
    if ((email.value.length > 0) && (password.value.length > 0)) {
        return true;
    } else {
        alert('Please plete the form!');
        return false;
    }
}

function check(form) {

    var emailArray = ("[email protected]", "");
    var passwordArray = ("MyLogIn", "");

    if (email.value == "email" && password.value == "password") {
        window.open('myaccount.html');
    } else {
        alert('Please enter correct username or password!');
        return false;
    }   
}

function init() {
    'use strict';

    // Confirm that document.getElementById() can be used:
    if (document && document.getElementById) {
        var loginForm = document.getElementById('lgform');
        loginForm.onsubmit = validateForm;
    }
}
window.onload = init;

HTML:

<form action="" method="post" id="lgform">
    <fieldset>
        <legend>Login</legend>
        <div>
            <label for="email">Email Address</label>
            <input type="email" name="email" id="email" required>
        </div>
        <div>
            <label for="password">Password</label>
            <input type="password" name="password" id="password" required>
        </div>
        <div>
            <label for="submit"></label>
            <input type="submit" value="Login" id="submit">
        </div>
    </fieldset>
</form>
Share Improve this question edited Oct 27, 2013 at 23:15 user2625787 asked Oct 27, 2013 at 23:04 DedeDede 552 gold badges2 silver badges8 bronze badges 6
  • 3 Keep in mind that what you are trying to do is very insecure. No authentication logic should be done in the client side. Anyone can just look at your source code and see all the user names and passwords easily. – Guilherme Sehn Commented Oct 27, 2013 at 23:05
  • I hope that this code will not be appear in production. – Mindbreaker Commented Oct 27, 2013 at 23:06
  • This is a terrible idea. You need to process the login on the server side and issue an appropriate HTTP response if the login is successful. Most likely an HTTP 302. – Eric Schoonover Commented Oct 27, 2013 at 23:06
  • Homework question I'm guessing. – m.edmondson Commented Oct 27, 2013 at 23:08
  • The hard coding will not appear in production. – Dede Commented Oct 27, 2013 at 23:40
 |  Show 1 more ment

1 Answer 1

Reset to default 1

Hardcoding the username and password client side is very dangerous - don't do it.

In order to redirect you simply:

window.location = "http://www.google./"

however I very much doubt the page you are directing too is secure, can it be accessed directly via the url without passing your authentication?

本文标签: How to redirect to another page using javascript after successful loginStack Overflow