How To Make A Login Page In Html Using Javascript

Creating a login page in HTML using JavaScript is a fundamental skill that every web developer should have. In this article, I will guide you through the process of building a simple and secure login page while adding some personal touches and commentary along the way.

Setting Up the HTML Structure

To start, let’s create the basic HTML structure for our login page. Open your favorite code editor and create a new HTML file. Inside the <body> tag, we will add a <form> element to hold our login form, like this:

<form id="login-form">


Within the form, we will add the necessary form elements such as <input> fields for the username and password, and a <button> to submit the form:

<form id="login-form">
<label for="username">Username:</label>
<input type="text" id="username" required>

<label for="password">Password:</label>
<input type="password" id="password" required>

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

Feel free to add your own personal touches to the form and HTML structure to make it unique to your website’s design and branding.

Adding JavaScript for Form Validation

Now that we have our HTML structure in place, let’s add some JavaScript code to handle form validation. We want to make sure that the username and password fields are not empty before submitting the form.

const form = document.getElementById('login-form');
form.addEventListener('submit', function(event) {
event.preventDefault(); // Prevent the default form submission

const username = document.getElementById('username').value;
const password = document.getElementById('password').value;

if (username.trim() === '' || password.trim() === '') {
alert('Please enter both username and password.'); // Display an error message
} else {
// Perform the login logic here
alert('Login successful!'); // Display a success message
form.reset(); // Reset the form

In the above JavaScript code, we are listening for the form’s submit event and preventing the default form submission using event.preventDefault(). Next, we retrieve the values of the username and password fields using their respective id attributes. We then check if either of the fields is empty and display an error message if they are. If both fields have values, you can add your own logic to validate the credentials on the server or perform any necessary authentication actions. In this example, we simply display a success message and reset the form.

Styling the Login Page

No login page is complete without some stylish design. Feel free to add your own CSS or use a framework like Bootstrap to make your login page visually appealing. Customize the colors, fonts, and layout to match your website’s overall theme. Remember, first impressions matter!


Creating a login page in HTML using JavaScript is an essential skill for web developers. In this article, we explored the steps involved in setting up the HTML structure, adding JavaScript for form validation, and styling the login page to make it visually appealing. Remember to always prioritize security and user experience when designing login pages for your website. Now, go ahead and implement your own login page with your personal touches and let your creativity shine!