neustre
neustre

Reputation: 107

go to another page html with javascript

I have two files. One is login.html which is a simple html5 file with a form.

HTML

<form method="post" action="" name="form1">entre the pasword:
    <input type="password" name="code" placeholder="code" maxlength="6">
    <p class="submit">
        <input type="submit" name="commit" value="send" onclick="verif(document.form1.code)">
    </p>
</form>

Second is my javascript file with the below code:

function verif(inputtxt) {

    var pwd = "123456";
    if (inputtxt.value.match(pwd)) {
        window.location.href = 'Test.html';
    } else {
        alert('Code erron\351 ! ')
        return false;
    }

}

Now my problem is that when I enter my password, if it is wrong the alert message indicating an error should appear (it appears and I don't have a problem with that) and if it is correct, I should get redirected to the next page. The second part doesn't work for me.

Please help, I'm stuck with that for two days now..

Upvotes: 1

Views: 21000

Answers (1)

Harry
Harry

Reputation: 89750

Since your button is a submit button, I think it is submitting the form after the JS is done and this could be the reason why you don't get redirected to Test.html (as form action attribute doesn't have any value.) Try the below code for the HTML form and check if this solves the issue.

<form method="post" action="" name="form1" onsubmit="verif(document.form1.code);return false;">entre the pasword:
    <input type="password" name="code" placeholder="code" maxlength="6">
    <p class="submit">
        <input type="submit" name="commit" value="send">
    </p>
</form>

The return false; in the onsubmit attribute prevents the form's default submit action. The verif(document.form1.code) will be executed whenever the form is submitted (that is the submit button is clicked).

Upvotes: 3

Related Questions