Solution 1 :

You need to have a button type of submit in order to be able to submit your form by clicking the button.

<button type="submit">SUBMIT</button>

By default, button’s have a type of submit, so you can even get away with leaving this attribute off your button.


Solution 2 :

Actually you are calling your validate function on “onsubmit” so you have to have button of type submit

<button type="submit">SUBMIT</button>

Without button of type submit your function won’t be called.

Solution 3 :

Your function is not getting called because your button type is set to button.


<button type="button">SUBMIT</button>


<button type="submit">SUBMIT</button>

Problem :

After entering the correct username and password I’m not getting forwarded to ‘another.html’. How to resolve this ? Any suggestions ? [Extra text to reach word limit cause otherwise the question won’t get post]

<html lang="en">
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script type="text/javascript" language="javascript">
        document.writeln("<h1 id= 'heading' style='color:blue;background-color:yellow'><center>Welcome to my website</center></h1>");
        function validate()
            var username = document.getElementById("username");
            var password = document.getElementById("password");

            if(username.value == "username" && password.value == "password")
                return true;
                alert("Please enter all fields");
                return false;
    <form onsubmit="return validate()" action="another.html">
        <input id="username" type="text" name="username" placeholder="username" /><br/><br/>
        <input id="password" type="password"name="password" placeholder="password" /><br/><br/>
        <button type="button">SUBMIT</button>


Leave a Reply

Your email address will not be published. Required fields are marked *