May
10

Design a Login Form with Password Toggle in JavaScript

05/10/2025 12:00 AM by Design a Login Form with Password Toggle in JavaScript in Html


login form

 

Creating a user-friendly login form is a cornerstone of modern web development. Adding a password toggle feature enhances usability by allowing users to view their password, reducing errors during login. In this comprehensive guide, we’ll walk through designing a sleek login form with a password toggle using HTML, CSS, and JavaScript. We’ll include practical examples, free AI tools for design inspiration, and a complete code snippet for easy implementation. By the end, you’ll have a fully functional login form ready to integrate into your projects.

Why Include a Password Toggle in Your Login Form?

A password toggle (often represented by an eye icon) allows users to switch between hiding and showing their password. This feature improves user experience by:

  • Reducing Errors: Users can verify their input, especially for complex passwords.
  • Enhancing Accessibility: It helps users with visual impairments or those typing on mobile devices.
  • Improving Usability: It provides a seamless and intuitive interaction.

Let’s dive into building a login form with this feature, starting with the structure.


Step 1: Setting Up the HTML Structure

The HTML will define the form’s layout, including input fields for email/username, password, and a toggle button. We’ll use semantic HTML for accessibility and structure.

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Login Form with Password Toggle</title>
  <link rel="stylesheet" href="styles.css">
</head>
<body>
  <div class="login-container">
    <h2>Login</h2>
    <form id="login-form">
      <div class="input-group">
        <label for="email">Email or Username</label>
        <input type="text" id="email" name="email" required>
      </div>
      <div class="input-group">
        <label for="password">Password</label>
        <div class="password-container">
          <input type="password" id="password" name="password" required>
          <span class="toggle-password">👁️</span>
        </div>
      </div>
      <button type="submit">Login</button>
    </form>
  </div>
  <script src="script.js"></script>
</body>
</html>

This code creates a simple form with:

  • An email/username input.
  • A password input wrapped in a container for the toggle icon.
  • A submit button.
  • External CSS and JS files for styling and functionality.

Step 2: Styling the Form with CSS

The CSS will make the form visually appealing and responsive. We’ll use a clean, modern design with a centered layout and subtle animations.

body {
  font-family: Arial, sans-serif;
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  margin: 0;
  background-color: #f0f2f5;
}

.login-container {
  background: white;
  padding: 2rem;
  border-radius: 8px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  width: 100%;
  max-width: 400px;
}

h2 {
  text-align: center;
  margin-bottom: 1.5rem;
  color: #333;
}

.input-group {
  margin-bottom: 1rem;
}

label {
  display: block;
  margin-bottom: 0.5rem;
  color: #555;
}

input {
  width: 100%;
  padding: 0.75rem;
  border: 1px solid #ddd;
  border-radius: 4px;
  font-size: 1rem;
}

.password-container {
  position: relative;
}

.toggle-password {
  position: absolute;
  right: 10px;
  top: 50%;
  transform: translateY(-50%);
  cursor: pointer;
  user-select: none;
}

button {
  width: 100%;
  padding: 0.75rem;
  background-color: #007bff;
  border: none;
  border-radius: 4px;
  color: white;
  font-size: 1rem;
  cursor: pointer;
  transition: background-color 0.3s;
}

button:hover {
  background-color: #0056b3;
}

@media (max-width: 480px) {
  .login-container {
    padding: 1.5rem;
    margin: 1rem;
  }
}

This CSS:

  • Centers the form on the page with a subtle shadow.
  • Styles inputs and labels for clarity.
  • Positions the toggle icon inside the password field.
  • Adds responsiveness for mobile devices.

Step 3: Adding the Password Toggle with JavaScript

The JavaScript will handle the toggle functionality, switching the password input between type="password" and type="text".

document.addEventListener('DOMContentLoaded', () => {
  const passwordInput = document.getElementById('password');
  const togglePassword = document.querySelector('.toggle-password');

  togglePassword.addEventListener('click', () => {
    const type = passwordInput.getAttribute('type') === 'password' ? 'text' : 'password';
    passwordInput.setAttribute('type', type);
    togglePassword.textContent = type === 'password' ? '👁️' : '🙈';
  });

  // Optional: Basic form validation
  const form = document.getElementById('login-form');
  form.addEventListener('submit', (e) => {
    e.preventDefault();
    alert('Form submitted! (This is a demo)');
  });
});

This script:

  • Waits for the DOM to load.
  • Toggles the password input type when the eye icon is clicked.
  • Updates the icon to reflect the visibility state.
  • Includes a basic form submission handler for demonstration.

Enhancing the Form with Free AI Tools

To make your login form stand out, consider using free AI tools for design and prototyping:

  1. Figma (Free Plan): Design your form’s UI with Figma’s collaborative interface. Figma offers free templates for login forms.
  2. Canva: Create custom icons or backgrounds for your form. Canva has a free tier with plenty of design assets.
  3. Photopea: A free alternative to Photoshop for editing SVGs or creating custom graphics. Photopea runs in your browser.
  4. Coolors: Generate color schemes for your form’s palette. Coolors is free and intuitive.

These tools can help you prototype and refine your design before coding.


Best Practices for Login Forms

To ensure your login form is both secure and user-friendly, follow these best practices:

  • Use HTTPS: Ensure your form submits data over a secure connection.
  • Validate Inputs: Implement client-side and server-side validation to prevent invalid submissions.
  • Accessibility: Use ARIA labels and ensure keyboard navigation works.
  • Rate Limiting: Protect against brute-force attacks by limiting login attempts.
  • Clear Feedback: Provide users with clear error messages (e.g., “Invalid email or password”).

Complete Code

Below is the complete code for the login form, combining HTML, CSS, and JavaScript.

<!doctypehtml><html lang=en><meta charset=UTF-8><meta content="width=device-width,initial-scale=1"name=viewport><title>Login Form with Password Toggle</title><style>body{font-family:Arial,sans-serif;display:flex;justify-content:center;align-items:center;height:100vh;margin:0;background-color:#f0f2f5}.login-container{background:#fff;padding:2rem;border-radius:8px;box-shadow:0 4px 12px rgba(0,0,0,.1);width:100%;max-width:400px}h2{text-align:center;margin-bottom:1.5rem;color:#333}.input-group{margin-bottom:1rem}label{display:block;margin-bottom:.5rem;color:#555}input{width:100%;padding:.75rem;border:1px solid #ddd;border-radius:4px;font-size:1rem}.password-container{position:relative}.toggle-password{position:absolute;right:10px;top:50%;transform:translateY(-50%);cursor:pointer;user-select:none}button{width:100%;padding:.75rem;background-color:#007bff;border:none;border-radius:4px;color:#fff;font-size:1rem;cursor:pointer;transition:background-color .3s}button:hover{background-color:#0056b3}@media (max-width:480px){.login-container{padding:1.5rem;margin:1rem}}</style><div class=login-container><h2>Login</h2><form id=login-form><div class=input-group><label for=email>Email or Username</label> <input id=email name=email required></div><div class=input-group><label for=password>Password</label><div class=password-container><input id=password name=password required type=password> <span class=toggle-password>👁️</span></div></div><button type=submit>Login</button></form></div><script>document.addEventListener('DOMContentLoaded', () => {
      const passwordInput = document.getElementById('password');
      const togglePassword = document.querySelector('.toggle-password');

      togglePassword.addEventListener('click', () => {
        const type = passwordInput.getAttribute('type') === 'password' ? 'text' : 'password';
        passwordInput.setAttribute('type', type);
        togglePassword.textContent = type === 'password' ? '👁️' : '🙈';
      });

      const form = document.getElementById('login-form');
      form.addEventListener('submit', (e) => {
        e.preventDefault();
        alert('Form submitted! (This is a demo)');
      });
    });</script>

Designing a login form with a password toggle is a straightforward yet impactful way to enhance your web application’s user experience. By combining HTML for structure, CSS for styling, and JavaScript for interactivity, you can create a professional and accessible form. Use free AI tools like Figma, Canva, or Coolors to refine your design, and follow best practices to ensure security and usability. The complete code and SVG illustration provided make it easy to get started.

Experiment with this form, customize its styles, and integrate it into your next project you can test this code on our page Here