4 - Password Strength Checker

Description

Develop a React application that functions as a password strength checker. Users should be able to enter a password in a text field. The application will analyze the password's strength based on various criteria like length, character types (uppercase, lowercase, numbers, special characters), and presence of dictionary words. The strength analysis should be visually communicated to the user through a progress bar, color indicators, or descriptive messages.

Algorithm

  1. User Input: Capture the user's password entry using a controlled <input> component with event handlers for state updates.

  2. Strength Calculation: Define a function that analyzes the password and assigns a strength score based on pre-defined criteria. Consider factors like:

    • Length: Longer passwords are generally stronger.

    • Character types: Including uppercase, lowercase, numbers, and special characters increases strength.

    • Dictionary words: Avoid common words and phrases found in dictionaries for stronger passwords.

  3. Visual Feedback: Utilize UI elements like:

    • Progress bar: Visually depict the calculated strength score using a progress bar ranging from weak to strong.

    • Color indicators: Employ color coding (e.g., green for strong, yellow for moderate, red for weak) to represent the password's strength level.

    • Descriptive messages: Provide textual feedback explaining the strength assessment and suggesting improvement tips for weak passwords.

Code

import React, { useState } from 'react';

function PasswordStrengthChecker() {
  const [password, setPassword] = useState('');
  const [strength, setStrength] = useState(0);

  const calculateStrength = (password) => {
    let score = 0;
    if (password.length > 8) score += 2;
    if (/[A-Z]/.test(password)) score += 1;
    if (/[a-z]/.test(password)) score += 1;
    if (/\d/.test(password)) score += 1;
    if (/[^a-zA-Z0-9]/.test(password)) score += 1;
    
    setStrength(score);
  };

  const handleChange = (event) => {
    setPassword(event.target.value);
    calculateStrength(event.target.value);
  };

  const getStrengthLabel = () => {
    if (strength < 3) return 'Weak';
    if (strength < 6) return 'Moderate';
    return 'Strong';
  };

  const getProgressBarColor = () => {
    if (strength < 3) return 'red';
    if (strength < 6) return 'yellow';
    return 'green';
  };

  return (
    <div className="password-strength-checker">
      <h1>Password Strength Checker</h1>
      <input
        type="password"
        placeholder="Enter your password"
        value={password}
        onChange={handleChange}
      />
      <div className="strength-meter">
        <progress value={strength} max={6} style={{ backgroundColor: getProgressBarColor() }} />
        <span>{getStrengthLabel()}</span>
      </div>
    </div>
  );
}

export default PasswordStrengthChecker;

Explanation

  • useState Hooks: Manage state for user input (password) and calculated strength (strength).

  • Strength Calculation: The calculateStrength function evaluates the password based on length and presence of different character types, awarding points for each criterion.

  • Visual Feedback:

    • Progress bar: Displays the strength score on a progress bar, dynamically changing its color (getProgressBarColor) based on the score.

    • Descriptive messages: The getStrengthLabel function dynamically returns a label ("Weak", "Moderate", or "Strong") based on the score.

Additional Notes

  • This code provides a basic example of a password strength checker. You can enhance it by:

    • Implementing more sophisticated strength criteria like dictionary word checks and pattern matching.

    • Providing detailed feedback messages suggesting specific ways to improve weak passwords.

    • Offering password generator functionality to suggest secure passwords.

Last updated