Coder Social home page Coder Social logo

password_generator's Introduction

JavaScript: Password Generator

The goal of this project is to make a randomly generated password that a range of selected user criteria and allows them to smoothly and easily get a fresh and secure password.

Password Generator

Table of Contents

Technologies_Used

  • HTML (27.3%)
  • CSS (16.4%)
  • Javascript (56.3%)

Deployed Site

Responsive Portfolio

Features

1. A series of checkboxes for setting password criteria

Why?

To allow users to determine how secure they want their passwords. For instance, some security prompts require special characters and some don't allow them. We wanted to create a password generator that any employee could use to secure information regardless of the specific requirements of their application. One generator to rule them all.

What?/How?

First I started by creating the checkboxes in the html file:

   <label>Password length  <input type="number" id="length" min='4' max='20' value='20' /></label>  
   <label>Include uppercase letters  <input type="checkbox" id="uppercase"  class= "checkmark" checked /></label>       
   <label>Include lowercase letters <input type="checkbox" id="lowercase" class= "checkmark" checked /></label> 
   <label>Include numbers<input type="checkbox" id="numbers" class= "checkmark" checked /></label> 
   <label>Include special characters <input type="checkbox" id="symbols" class= "checkmark" checked /></label> 

Then I created the variables to get the checkboxes by id:

const lengthEl = document.getElementById('length');
const uppercaseEl = document.getElementById('uppercase');
const lowercaseEl = document.getElementById('lowercase');
const numbersEl = document.getElementById('numbers');

and Finally I made sure I was able to listen to see if they are checked:

generateEl.addEventListener('click', () => {

  const length = +lengthEl.value;

  //have the other boxes been checked?
  const hasLower = lowercaseEl.checked;
  const hasUpper = uppercaseEl.checked;
  const hasNumber = numbersEl.checked;
  const hasSpecial = specialEl.checked;

resultEl.innerText = generatePassword( 
  hasLower, 
  hasUpper, 
  hasNumber, 
  hasSpecial, 
  length)
})

2. Validation for correct Input

Why?

This feature does feed into the others but I felt it important to address this issue separately. Before completely this projects, I saw a lot of code examples and projects similar to mine that did not validate and accepted incorrect input. I chose to build my validation in but I do know there are people that consider checking for a correct answer very different than checking for an answer.

What?/How?

In order to have a customizable password, selections need to be made before the password is generated. Therefore, when the button "Generate Password" is clicked a prompt comes up allows the user to input their specific criteria. If they don't enter any valid criteria then they alerted and sent back to the previous prompt to enter in valid criteria. If they do not select some combination of upper case, lower case, numbers and special characters they will be prompted again.

3. A Generated password that meets all criteria

Why?

If you are going to create something it really should meet all the criteria, shouldn't it? I don't want to create a password generator that doesn't do what it promises to do.

What?/How?

In order to validate that the boxes are checked I created a typeCont constant that tracks how many are checked, if none are being passed through then typesCount is equal to zero and instead of a password, text asking for the boxes to be checked is returned.

function generatePassword(lower, upper, number, special, length) {
let generatedPassword = '';
  const typesCount = lower + upper + number + special

  if(typesCount === 0) {
    return 'Please check at least 1 box';
  }
}

I used String.fromCharCode and looked up the CharCodes for each element. For lower case letters the code number starts at 97, so if we grow through 26 letters + 97 we will have gone through all of the lower case letters and randomly selected them.

function getRandomLower() {
  //97 to 122 is are lowercase letters
 return  String.fromCharCode(Math.floor(Math.random() *26) + 97)
}
function getRandomUpper() {
  return String.fromCharCode(Math.floor(Math.random() *26) + 65)
}

function getRandomNumbers() {
  return String.fromCharCode(Math.floor(Math.random() *10)+ 48)
}

function getRandomSpecial() {
  const specials = '!@#$%^&*(){}[]-_=,.<>?/|;:~`'
  return specials[Math.floor(Math.random() * specials.length)];
}

These functions are then set to equal their respective groups:

 randomFunc = {
  lower: getRandomLower,
  upper: getRandomUpper,
  number: getRandomNumbers,
  special: getRandomSpecial
}

After checking whether or not the box is checked, we then set those items into an array that will tell us what section (upper, lower, number or special) to go into to retrieve the char. This array is then iterated through based off of the desired length and builds the password using the Object.keys() method. Then it is turned into an array again and passed through the shuffle array function. Finally, it set back into a string using the join(") method and set the final Password.

function generatePassword(lower, upper, number, special, length) {
let generatedPassword = '';
  const typesCount = lower + upper + number + special
  const typesArray = [{lower}, {upper}, {number}, {special}].filter(item => Object.values(item)[0])

  if(typesCount === 0) {
    return 'Please check at least 1 box';
  }

for (let i =0; i < length; i+=typesCount) {
  typesArray.forEach(type => {
    const funcName = Object.keys(type)[0];
    generatedPassword += randomFunc[funcName]();
 
  });
}
// creating an array
const passArray = [...generatedPassword.slice(0, length)];
// shuffling password array
const shufflePassword = shuffleArray(passArray);
// returning password array to a string and setting it to the final value
const finalPassword = shufflePassword.join("");

console.log(finalPassword)
return finalPassword
}

4. A button

Why?

I suppose we need a button to get started on the prompts. It is pretty annoying when you open a webpage and a prompt immediately comes up without you asking it to. The button allows to loop through as well. We can start and then if we need to we can start all over again. It will rewrite passwords to our hearts content.

What?How?

There are 3 separate parts of a button in java script that connect to an additional part in html.

in html are code looks like this:

'Generate Password'

It contains an id, a class and a area all encompassed in it's very own special button tag.

There are two ways to call a button and make it work in Javascript but I will only explain the way I did it.

First, I created a variable called generateBtn. I then set it equal to 'document.querySelector("#generate");' The #generate refers back to the id given in the html. We now have that very vital connection where our button is going to begin to think about doing a thing.

Second step is to make content -I need something for this button to do so when the button is pushed it needs to write to the document (html) in the text area called (id) password. This is is where our password will show up. We use the variable, pass to hold the password. 'document.getElementById('password').value = pass;'

'generateBtn.addEventListener("click", writePassword);'

Finally, we have to tell the button to listen. What to listen for? A click! What are we doing upon click? writing the password!

5. Copy Button

Why?

If we are generating a password, usually we are generating it in order to use it in some other application. This button improves usabilty by allowing the user to move the password into a clipboard rather than selecting it and hitting ctrl+ copy or, god forbid, reading it off of the screen and then typing it again.

What?How?

  document.execCommand('copy');
  textarea.remove()
  alert('password copied to clipboard');

Usage

This is meant for employees who need to generate secure passwords to be used to access sensitive data.

Author

Rachael Kelm-Southworth

Credits

I would like to thank Kerwin, Manuel, Roger, Jerome and all my classmates for helping me understand this subject matter and anyone that contributed to make the base code.

License

MIT

password_generator's People

Contributors

rksouth avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.