About this snippet

Provide real-time feedback on password strength as a user types. Checks for length, numbers, uppercase, and special characters.

Size
3.7 KB
Uses
HTML, CSS, JavaScript
Loads
Nothing external
Views
348
<div class="input-group">
  <label for="password">Password</label>
  <div class="password-wrapper">
    <input type="password" id="password" name="password" placeholder="Min. 8 characters">
    <button type="button" id="togglePassword" aria-label="Toggle password visibility">
      <span id="eyeIcon"><svg width="16" height="16" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M12,9A3,3 0 0,1 15,12A3,3 0 0,1 12,15A3,3 0 0,1 9,12A3,3 0 0,1 12,9M12,4.5C17,4.5 21.27,7.61 23,12C21.27,16.39 17,19.5 12,19.5C7,19.5 2.73,16.39 1,12C2.73,7.61 7,4.5 12,4.5M3.18,12C4.83,15.36 8.24,17.5 12,17.5C15.76,17.5 19.17,15.36 20.82,12C19.17,8.64 15.76,6.5 12,6.5C8.24,6.5 4.83,8.64 3.18,12Z" /></svg></span>
    </button>
  </div>
  
  <div id="strengthIndicator">
    <div class="bar-container">
      <div id="strengthBar"></div>
    </div>
    <div class="status-row">
      <span id="strengthText">Enter password</span>
      <span id="strengthHint"></span>
    </div>
  </div>
</div>
:root {
  --bg-soft: #f8fafc;
  --border-color: #e2e8f0;
  --primary-accent: #6366f1;
  --text-main: #1e293b;
  --text-muted: #64748b;
  --transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

* {
  font-family: system-ui, "Segoe UI", Roboto, Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
  box-sizing: border-box;
}

.input-group {
  max-width: 320px;
  margin: 2rem auto;
}

label {
  display: block;
  font-weight: 600;
  margin-bottom: 8px;
  color: var(--text-main);
}

.password-wrapper {
  position: relative;
  display: flex;
  align-items: center;
}

#password {
  width: 100%;
  padding: 12px 16px;
  border: 2px solid var(--border-color);
  border-radius: 12px;
  font-size: 1rem;
  transition: var(--transition);
  outline: none;
}

#password:focus {
  border-color: var(--primary-accent);
  box-shadow: 0 0 0 4px rgba(99, 102, 241, 0.1);
}

#togglePassword {
  position: absolute;
  right: 12px;
  background: none;
  border: none;
  cursor: pointer;
  font-size: 1.2rem;
  opacity: 0.6;
}

/* Strength Meter */
#strengthIndicator {
  margin-top: 12px;
}

.bar-container {
  height: 6px;
  background: var(--border-color);
  border-radius: 10px;
  overflow: hidden;
  margin-bottom: 8px;
}

#strengthBar {
  height: 100%;
  width: 0%;
  border-radius: 10px;
  transition: width 0.5s ease, background-color 0.3s ease;
}

.status-row {
  display: flex;
  justify-content: space-between;
  font-size: 0.85rem;
  font-weight: 500;
}

#strengthText { color: var(--text-muted); }
const password = document.getElementById('password');
const bar = document.getElementById('strengthBar');
const text = document.getElementById('strengthText');
const toggleBtn = document.getElementById('togglePassword');

// Toggle Visibility
toggleBtn.addEventListener('click', () => {
  const type = password.getAttribute('type') === 'password' ? 'text' : 'password';
  password.setAttribute('type', type);
});

// Strength Logic
password.addEventListener('input', () => {
  const val = password.value;
  let score = 0;

  if (val.length > 6) score++;
  if (/[A-Z]/.test(val)) score++;
  if (/[0-9]/.test(val)) score++;
  if (/[^A-Za-z0-9]/.test(val)) score++;

  const config = [
    { width: '5%', color: '#cbd5e1', label: 'Very Weak' },
    { width: '25%', color: '#ef4444', label: 'Weak' },
    { width: '50%', color: '#f59e0b', label: 'Fair' },
    { width: '75%', color: '#3b82f6', label: 'Good' },
    { width: '100%', color: '#10b981', label: 'Strong' }
  ];

  const result = val.length === 0 ? { width: '0%', color: '#e2e8f0', label: 'Enter password' } : config[score];

  bar.style.width = result.width;
  bar.style.backgroundColor = result.color;
  text.innerText = result.label;
  text.style.color = val.length === 0 ? '#64748b' : result.color;
});
Esc
↑↓ moveEnter select

Start from a template

Open an .html file, or HTML, CSS, JS, Markdown, Less or TypeScript files. You can also drop them here. Styles and scripts go into their panes.

Share

Source

Keyboard shortcuts

Save