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.
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
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>
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 =
"!@#$%^&*()_+-=[]{}|;:,.?";
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;
}
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.
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];
}
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;
}
Display the Generated Password
Once the password is generated, update the input field using the value property.
document.getElementById(
"password"
).value = password;
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
);
}
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";
}
Complete Project Flow
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