Creating phony passwords effortlessly in JavaScript utilizing the informal library

I am trying to create a password that meets the following criteria:

  • Minimum length: 7
  • Maximum length: 15
  • At least one uppercase letter
  • At least one lowercase letter
  • Special characters: ~ ! @ # $ % ^ * ( ) _ + ?

I have been using the casual library for generating passwords, but I'm having trouble including special characters. Can anyone provide assistance with this problem?

var casual = require('casual');
var password = casual.password.substring(10,{

    length: 10,
    uppercase: true,
    lowercase: true,
    number: true,
    symbols: true,
    strict: true
});

Answer №1

Creating your custom password generator is quite simple:

const getLowerCase = () => casual.random_element('ABCDEFGHIJKLMNOPQRSTUVWXYZ'.split(''));
const getUpperCase = () => casual.random_element('abcdefghijklmnopqrstuvwxyz'.split(''));
const getSpecialChar = () => casual.random_element('~!@#$%^*()_+'.split(''));
const getAnyCharacter = () => casual.random_element([getLowerCase(), getUpperCase(), getSpecialChar()]);

casual.define('customPassword', function() {
    const length = casual.integer(from = 7, to = 15);
    const password = [getLowerCase(), getUpperCase(), getSpecialChar()];
    
    while (password.length < length) {
       password.push(getAnyCharacter());
    } 
    return password.join('');
});

If security is a concern, remember to shuffle the password array before returning it.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What strategies can I use to organize and fuse together my library?

I am intrigued by the concept of modular JS and have decided to create my own small library to experiment with it. Here is my vision for how I want it to function: It will include 5 methods Users can download a full library that exports a global variab ...

What is the best way to capture source code and store it in a text file?

Can you assist me in extracting the source code of a webpage and saving it into a text file? My goal: Instead of going through the process of right-clicking on the page, selecting view source code, copying, and pasting into a text file... I want to simpl ...

"By setting the HTML input box to read-only, the JavaScript button has the

Check out this js fiddle demonstration: http://jsfiddle.net/YD6PL/110/ Here is the HTML code snippet: <input type="text" value="a" readonly> <input type="text"> <input type="text"> <div> <button class="buttons">c</button ...

Unable to view the token balances of the smart contract on remix while executing the seeBalance function

pragma solidity =0.7.6; pragma abicoder v2; import "https://github.com/Uniswap/v3-periphery/contracts/interfaces/ISwapRouter.sol"; interface IERC20 { function balanceOf(address account) external view returns (uint256); function transfer(address ...

Issue: The system is unable to locate the module labeled './lib/binding/napi-v3/argon2.node'

After attempting to install bcrypt or argon2 with the command npm install --ignore-scripts --omit=dev, an error occurred: app-1 | node:internal/modules/cjs/loader:998 app-1 | throw err; app-1 | ^ app-1 | app-1 | Error: Cannot find modul ...

Verify whether the input includes a specific value or a different one

Can someone help me with a simple task of checking if a textarea contains the value "12" OR "34"? I have tried the code below but it doesn't seem to work. Any suggestions? function check() { if (V1.value == ('12' || '34')) { ...

What is the main file that vue-cli-service utilizes for entry?

Interested in a project utilizing vue, webpack, babel, npm? You can launch it with npm run server. While exploring how this command functions, I came across vue-cli-service serve in the package.json file. But how exactly does vue-cli-service initialize ...

Utilizing Mantine dropzone in conjunction with React Hook Form within a Javascript environment

Can Mantine dropzone be used with React hook form in JavaScript? I am currently working on a modal Upload using Tailwind components like this import { useForm } from 'react-hook-form'; import { Group, Text, useMantineTheme } from '@mantine/c ...

Unable to modify a variable within a request

Having trouble updating a variable within a request? It seems like the variable doesn't change when it should. const request = require("request"); var all = JSON.parse(body); var steamplayer = all['response']['players']['play ...

What are the consequences of altering the DOM in React?

As a beginner react developer, I am currently working on creating a MERN App. I have a question for the community regarding my project where I needed to make several changes to the DOM as illustrated below: document.getElementsByTagName('body')[ ...

Determine the maximum array size in Javascript

Having trouble setting a limit in my custom lightbox for a gallery <script> var imagenumber = 0; function btnleft(){ load = imagenumber-=1; document.getElementById('lightboxcontent').innerHTML=imagelist[load]; ...

Arrange arrays with multiple dimensions in JavaScript based on their ranges

Seeking assistance on sorting a multi-dimensional array returned from an API to enable users to choose a range based on beats. I'm currently facing challenges with the data my API is returning. var myObj = [{ title: 'title one', be ...

Loop through JSON array within an angular controller

I am currently attempting to iterate through a JSON array and display the values on the frontend of my application. I have provided my code, but I'm having trouble retrieving specific values (startDate, endDate) from within the array and displaying th ...

Constructing hierarchical objects in JavaScript

I am looking to create a complex nested object in JavaScript that follows a specific structure. const data = { name: 'Context 1', children: [ { name: 'Option 1', children: [ { name: 'Context&ap ...

Incorporating npm modules into a Laravel 5.4 project

What is the proper way to add and configure npm packages for Laravel 5.4.* so that they work correctly? Background: I recently watched a tutorial on adding the NOTY npm library to Laravel 5.3, which utilized a gulpfile.js. How can this be adapted for Lara ...

Learn how to incorporate additional rows into a table by pressing the plus button within the table with the help of Angular

I require some assistance. I am looking to dynamically generate a row after clicking on the plus button using angular.js. The newly created row should contain an ID and model generated dynamically. Here is an overview of my code: <table class="table ta ...

ReactJS: An error occurred - TypeError: this.state.items.map is not a function

As a beginner in ReactJS, I am working on my first project which is a To-Do List with drag and drop feature, add, delete, and edit functionalities. I encountered an error while trying to map an array upon submitting a text to add an item to the items array ...

What steps does VSCode take to ensure that all necessary node dependencies are installed for its extensions?

As I work on constructing my own pluggable application, I've been curious about how vscode handles its extensions' dependencies. Does it run npm install in each extension directory and utilize those installations individually? Or does it have a c ...

The Azure DevOps npm build is including files that cannot be found locally

I encountered some errors when executing ng build --environment=sit --aot -ec --output-hashing=media on Azure DevOps: Interestingly, the same command runs without any errors locally: Upon further investigation, I realized that the problematic files are m ...

Issues with setting up Rider npm run configuration

I'm currently setting up a debug environment in Rider for a React application. I started by creating an empty react app using the command npx create-react-app and launching it with npm start in cmd, which was successful. Now, I want to run it within R ...