JAVASCRIPT PROJECT 11

Build a JavaScript Password Generator

In this project, you will learn how to generate random passwords using JavaScript strings, arrays, loops, Math.random() and DOM manipulation.

01

Project Introduction

A password generator creates random character combinations that can be used as password suggestions.

What You Will Learn

  • JavaScript strings
  • Arrays and character sets
  • Math.random()
  • Math.floor()
  • Loops
  • DOM manipulation
  • Clipboard API
  • Conditional statements
Characters Letters, numbers and symbols.
Length Decide how many characters to generate.
Random Select characters randomly.
02

Create the HTML Structure

Start with a password display, length control, options and buttons.

<input
    type="text"
    id="password"
    readonly
>

<input
    type="number"
    id="length"
    min="4"
    max="32"
    value="12"
>

<label>
    <input
        type="checkbox"
        id="uppercase"
        checked
    >
    Uppercase
</label>

<label>
    <input
        type="checkbox"
        id="numbers"
        checked
    >
    Numbers
</label>

<label>
    <input
        type="checkbox"
        id="symbols"
        checked
    >
    Symbols
</label>

<button onclick="generatePassword()">
    Generate Password
</button>
The readonly attribute prevents the generated password field from being accidentally edited.
03

Create Character Sets

Define the characters that can be used in the generated password.

Lowercase Characters

const lowercase =
    "abcdefghijklmnopqrstuvwxyz";

Uppercase Characters

const uppercase =
    "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

Numbers

const numbers =
    "0123456789";

Symbols

const symbols =
    "!@#$%^&*()_+-=[]{}|;:,.?";
04

Set Password Length

Read the desired password length from the input field.

const length =
    Number(
        document.getElementById("length").value
    );

Validate the Length

if(
    length < 4 ||
    length > 32
){

    alert(
        "Password length must be between 4 and 32."
    );

    return;

}
A reasonable minimum length helps avoid generating extremely short password suggestions.
05

Select a Random Character

Math.random() generates a random decimal between 0 and 1.

const index =
    Math.floor(
        Math.random() *
        characters.length
    );

const character =
    characters[index];

Math.floor() converts the random decimal into a valid array or string index.

06

Generate Multiple Characters

A loop can repeat the random character selection until the required password length is reached.

let password = "";

for(
    let i = 0;
    i < length;
    i++
){

    const index =
        Math.floor(
            Math.random() *
            characters.length
        );

    password +=
        characters[index];

}
Each loop iteration adds one randomly selected character to the password.
07

Add Generator Options

Users can choose whether uppercase letters, numbers or symbols should be included.

let characters =
    "abcdefghijklmnopqrstuvwxyz";


if(uppercaseChecked){

    characters +=
        "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

}


if(numbersChecked){

    characters +=
        "0123456789";

}


if(symbolsChecked){

    characters +=
        "!@#$%^&*()";

}

Prevent Empty Character Sets

if(characters.length === 0){

    alert(
        "Select at least one character type."
    );

    return;

}
08

Display the Generated Password

Once the password is generated, update the input field using the value property.

document.getElementById(
    "password"
).value = password;
value is used for form controls such as input fields.
09

Copy Password to Clipboard

The Clipboard API allows the generated password to be copied directly from the webpage.

navigator.clipboard.writeText(
    password
);

Complete Copy Function

async function copyPassword(){

    const password =
        passwordInput.value;

    if(!password){

        return;

    }

    await navigator.clipboard.writeText(
        password
    );

}
Clipboard access can depend on browser security rules and the page environment.
10

Add a Simple Password Strength Indicator

The project can estimate strength using length and the types of characters included.

let score = 0;


if(password.length >= 8){

    score++;

}


if(/[A-Z]/.test(password)){

    score++;

}


if(/[0-9]/.test(password)){

    score++;

}


if(/[^A-Za-z0-9]/.test(password)){

    score++;

}

Display Strength

if(score <= 1){

    strength = "Weak";

}
else if(score <= 3){

    strength = "Medium";

}
else{

    strength = "Strong";

}
This is an educational strength indicator, not a cryptographic security assessment.

Complete Project Flow

OPTIONS Select Types
LENGTH Choose Size
RANDOM Pick Characters
GENERATE Build Password
COPY Clipboard
12

Complete Password Generator

You have now learned how to build a password generator using strings, conditions, loops, random numbers and DOM manipulation.

Tutorial Completed! 🔐

Now open the complete working Password Generator and test the different options.

Run Complete Project Learn JavaScript Math