Skip to content
the poor coder

js password generator

Poor Man
Published
Reading time
4 min read
On this page

How to Create a Password Generator Using JavaScript#

If you're looking for a way to generate random passwords for your website, then a JavaScript password generator is a great solution. Here's how you can create one:

Step 1: Create the HTML Document#

First, let's create an HTML document with a button that will generate the password:

Text
<!DOCTYPE html>
<html>
<head>
  <title>JavaScript Password Generator</title>
</head>
<body>
  <button id="generate-password">Generate Password</button>
  <p id="password"></p>
  <!-- Include the script tag for your JavaScript file -->
  <script src="script.js"></script>
</body>
</html>

Step 2: Create the JavaScript Function#

Next, let's create a JavaScript function that will generate the random password:

Text
// Generate a random password
function generatePassword() {
  // Define variables for the characters to use in the password
  var uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  var lowercase = "abcdefghijklmnopqrstuvwxyz";
  var numbers = "0123456789";
  var symbols = "!@#$%^&*()_+-=[]{}|\\;:<>?,./";

  // Combine all the characters into one string
  var allCharacters = uppercase + lowercase + numbers + symbols;

  // Generate an empty string to store the password
  var password = "";

  // Loop through the desired length of the password
  for (var i = 0; i < 16; i++) {
    // Pick a random character from the combined string
    var randomIndex = Math.floor(Math.random() * allCharacters.length);
    var randomCharacter = allCharacters.charAt(randomIndex);

    // Add the random character to the password
    password += randomCharacter;
  }

  // Display the password on the page
  document.getElementById("password").innerHTML = password;
}

// Call the function when the button is clicked
document.getElementById("generate-password").addEventListener("click", generatePassword);

This code defines four variables for the different types of characters we want to include in the password: uppercase letters, lowercase letters, numbers, and symbols. It then combines all the characters into one string and loops through 16 times to pick a random character from the string and add it to the password.

The password is then displayed on the page when the button is clicked.

Step 3: Add Syntax Highlighting#

To make your code easier to read, you can add syntax highlighting using a library like highlight.js. Here's how you can do it:

  1. Include the highlight.js library in your HTML head:
Text
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/10.5.0/styles/default.min.css">
<script src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/10.5.0/highlight.min.js"></script>
  1. Wrap your code in <pre><code> tags:
Text
<pre><code class="javascript">// Your code goes here</code></pre>
  1. Initialize the highlight.js library:
Text
<script>hljs.initHighlightingOnLoad();</script>

Here's the final code with syntax highlighting:

Text
<!DOCTYPE html>
<html>
<head>
  <title>JavaScript Password Generator</title>
  <link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/10.5.0/styles/default.min.css">
  <script src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/10.5.0/highlight.min.js"></script>
</head>
<body>
  <button id="generate-password">Generate Password</button>
  <p id="password"></p>
  <!-- Include the script tag for your JavaScript file -->
  <script src="script.js"></script>
  <script>hljs.initHighlightingOnLoad();</script>
</body>
</html>

<pre><code class="javascript">// Generate a random password
function generatePassword() {
  // Define variables for the characters to use in the password
  var uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  var lowercase = "abcdefghijklmnopqrstuvwxyz";
  var numbers = "0123456789";
  var symbols = "!@#$%^&*()_+-=[]{}|\\;:<>?,./";

  // Combine all the characters into one string
  var allCharacters = uppercase + lowercase + numbers + symbols;

  // Generate an empty string to store the password
  var password = "";

  // Loop through the desired length of the password
  for (var i = 0; i < 16; i++) {
    // Pick a random character from the combined string
    var randomIndex = Math.floor(Math.random() * allCharacters.length);
    var randomCharacter = allCharacters.charAt(randomIndex);

    // Add the random character to the password
    password += randomCharacter;
  }

  // Display the password on the page
  document.getElementById("password").innerHTML = password;
}

// Call the function when the button is clicked
document.getElementById("generate-password").addEventListener("click", generatePassword);</code></pre>

Now you have a working JavaScript password generator with syntax highlighting!

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC