How To Make A Login In Form Page In Javascript

In this article, I will guide you through the process of creating a login form page using JavaScript. As a web developer, I have encountered numerous scenarios where implementing user authentication is a crucial aspect of a website or application. Having a well-designed and functional login page is not only important for security reasons but also for creating a seamless user experience.

Understanding the Basics

Before we dive into the code, let’s first understand the basic structure of a login form page. Typically, a login form consists of two input fields, one for the username or email and the other for the password. Additionally, we usually include a “Remember Me” checkbox, a “Forgot Password” link, and a “Sign In” button.

HTML Markup

Let’s start by setting up the HTML markup for our login form page:

<form id="loginForm">
    <label for="username">Username:</label>
    <input type="text" id="username" name="username" required>
    <label for="password">Password:</label>
    <input type="password" id="password" name="password" required>
    <label for="rememberMe">Remember Me:</label>
    <input type="checkbox" id="rememberMe" name="rememberMe">
    <a href="forgot-password.html">Forgot Password?</a>
    <button type="submit">Sign In</button>

Here, we have wrapped our form elements within a `

` tag and assigned an ID of “loginForm” for easy access in JavaScript. Each input element has an ID, name, and required attribute for validation purposes. The “Forgot Password?” link points to a separate page where users can reset their password if they forget it. Finally, the “Sign In” button triggers the form submission.

JavaScript Implementation

Now, let’s add some JavaScript logic to handle the form submission and perform the necessary actions. Here’s an example implementation:

const form = document.getElementById('loginForm');

form.addEventListener('submit', function(e) {
    e.preventDefault(); // Prevent form submission
    const username = document.getElementById('username').value;
    const password = document.getElementById('password').value;
    const rememberMe = document.getElementById('rememberMe').checked;
    // Add your authentication logic here
    // For demonstration purposes, let's just display an alert message
    if (username === 'admin' && password === 'password') {
        alert('Login successful!');
    } else {
        alert('Invalid username or password.');

In this script, we first retrieve the form element by its ID and attach an event listener to the form’s submit event. Within the event listener function, we prevent the default form submission behavior using the `preventDefault()` method. This allows us to handle the form data and perform custom actions.

Next, we retrieve the values of the username and password fields using their respective IDs. We also check the state of the “Remember Me” checkbox using the `checked` property. At this point, you can add your own authentication logic to verify the user’s credentials against a database or any other authentication mechanism.

For the sake of demonstration, I have included a simple if-else statement to check if the username is “admin” and the password is “password”. If the credentials match, an alert message saying “Login successful!” is displayed. Otherwise, an alert message saying “Invalid username or password.” is displayed.


Creating a login form page in JavaScript can be a fundamental task for web developers. It allows us to implement user authentication and provide a secure and personalized experience for our users. By following the steps outlined in this article, you now have the knowledge to create your own login form page and enhance it with your own personal touches.

Remember, security should always be a top priority when handling user authentication. Make sure to employ best practices, such as hashing and salting passwords, to protect user data. Additionally, always consider accessibility and usability when designing your login form to ensure a seamless experience for all users.

Now that you have the foundation, go ahead and create your own login form page using JavaScript. Happy coding!