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

I am encountering a problem with the app.patch() function not working properly. Although the get and delete functions are functioning as expected, the patch function seems to be

I am in the process of setting up a server that can handle CRUD operations. The Movie model currently only consists of one property, which is the title. Although I can create new movies, delete existing ones, and even search for a ...

Offering fields for modules, main, and browser that meet the needs of ESM, CommonJS, and bundlers

I have upgraded a number of my published npm packages to include both commonjs and esm builds. Some of these packages are meant for use in both node and the browser, and they are all compiled using webpack or rollup. Additionally, all of them are written i ...

I possess an array containing objects of different lengths depending on the chosen city. How can I pinpoint the element that contains an object with a specific property?

My dilemma arises from the fact that the length of an array depends on the selected city, making it impossible to select elements using an index. In this scenario, I need to devise a method to choose elements based on the value of one of their properties. ...

Organizing your code with precision

I'm struggling with a project due to the poorly formatted code, making it almost impossible to read. Despite my attempts with various plugins and tools in VIM, Netbeans, Sublime Text 2, and Eclipse, the situation only seems to worsen. If anyone has ...

The Chrome extension for the New Tab Page is taking the spotlight away from the address bar

It appears that with the latest version of Chrome, extensions that previously had the ability to override Chrome's New Tab Page and take focus away from the Omnibox no longer have this capability. Is there a different method now to give focus to an i ...

I am having trouble retrieving a JsonResult from an asp.net mvc controller using $resource in angular

I am new to Angularjs and trying to integrate it with asp.net mvc. I am facing an issue where I am unable to access an asp.net mvc controller to return a JsonResult using $resource in angular. Strangely, when I use $.getJson in JavaScript directly, it work ...

Encountering Error 100 when executing apt-get within Docker's environment, seeking resolution

I've been trying to dockerize my node.js application but encountered a problem with installing a system dependency called magicgraph. Despite using the "apt-get install" command in the Dockerfile like this: RUN apt-get install --force-yes -y graphicsm ...

How can I use Angular to bind the text entered in an `input` within one `ng-repeat` `div` to another `div` within a different `ng-repeat`?

I am trying to create a dynamic Angular-based webpage where input tags are connected to h3 tags in separate DIVs. Below is the setup of my HTML page (as seen on Plunker): <!DOCTYPE html> <html> <head> <style type="text/css> ...

Transferring information from offspring to parent

Greetings! I'm currently getting my feet wet with React and finding myself stuck on an issue regarding passing states. Is it possible for me to pass a child state to a parent component in order to selectively render other child components? ...

What is the best way to fetch d3 data from a service?

I've been exploring a tutorial on creating charts with d3, which can be found at: When it comes to loading data for use in d3, I typically rely on the following code snippet: d3.tsv("data.tsv", type, function(error, data) { The file "data.tsv" is c ...

Despite following all the correct steps, the tailwind CLI remains unresponsive. Additionally, the default button fails to display any content

view the document and my packages (image) Although it worked with the cdn, I'm puzzled why I can't use it properly with the cli ...

The electron program is unable to locate the package.json module

I am new to electron and attempting to run an express app for the first time. However, I encountered this error: Need assistance updating code Error: Cannot find module 'C:\package.json' at Module._resolveFilename (module.js:440:15) ...

What is the best way to provide Monaco editor's loader.js and its dependencies on a local server for my React project?

Currently, I have integrated Monaco Editor in my project by utilizing the npm package Monaco Editor. When I build and serve my code on localhost, I noticed that the Loader Script is being loaded from a Content Delivery Network (CDN). I am curious to know ...

Tips for showing a single progress message when uploading multiple files on eleme.io [vuejs]

Tech Stack: Utilizing Vuejs with element.eleme.io framework. Objective: To implement a feature that allows users to upload multiple files while displaying only one "in progress message". To handle image uploads, we are integrating . During the upload pr ...

Whenever I launch my React web application, I consistently encounter a white screen when attempting to access it on my phone

After developing my web app in ReactJS and deploying it to the server, I've noticed that sometimes the screen appears white for the first time after deployment. However, when I reload the page, the app runs normally. I am hosting the backend and front ...

After a loop, a TypeScript promise will be returned

I am facing a challenge in returning after all calls to an external service are completed. My current code processes through the for loop too quickly and returns prematurely. Using 'promise.all' is not an option here since I require values obtain ...

Console colors will not display when running an npm script in a Git bash terminal (mintty)

Getting Started In my programming journey, I decided to experiment with creating a small console program that prints colored text using chalk library in conjunction with NodeJS: console.log(require('chalk').yellow('yellow text')); Th ...

Customizing Webpack 4's Entry Point

Below is the layout of my project: -node_modules -src -client -js -styles -views -index.js -webpack.config.js -server -.babelrc -package -package-lock -README.md -webpack ...

When I attempt to run several promises simultaneously with Promise.All, I encounter an error

My code contains a series of promises, but they are not being executed as expected. Although the sequence is correct and functional, I have found that I need to utilize Promise.all in order for it to work properly. dataObj[0].pushScreen.map(item => { ...

Typescript on the client-side: what is the best way to eliminate circular dependencies when using the factory method design pattern?

In my code, I have implemented the factory method pattern. However, some instances using this pattern end up with circular dependencies. Removing these dependencies has proven to be a challenge for me. To illustrate, consider the following example: // fact ...