Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

144
Views
Generar una contraseña aleatoria con requisitos

Quiero generar una contraseña aleatoria de 16 caracteres que contenga números, alfabetos y caracteres especiales, pero no debe haber dos caracteres especiales adyacentes. Los caracteres especiales que estoy usando son ~!@#$%^&*()_+-={}[]:;\?,.|\\

 generateRandomPasswordSelection = function (length, chars) { var mask = ''; if (chars.indexOf('a') > -1) mask += 'abcdefghijklmnopqrstuvwxyz'; if (chars.indexOf('A') > -1) mask += 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; if (chars.indexOf('#') > -1) mask += '0123456789'; if (chars.indexOf('!') > -1) mask += '~!@#$%^&*()_+-={}[]:;\?,.|\\'; var result =''; for(var i=1; i>=1;i++){ result = ''; for (var i = length; i > 0; --i) result += mask[Math.round(Math.random() * (mask.length - 1))]; if(result.match(new RegExp(/(?!.*[~!@#$%^&*()_+={}[\]:;\?,.|\\-]{2})[A-Za-z0-9]{1,16}$/))){ console.log(result,'---------------->', 'BREAk') break; } } } generateRandomPasswordSelection(16,'aA#!');

Arriba está el código que probé, pero no funcionó. ¿Cómo lograr esto?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Utiliza la sintaxis ES6, con suerte más legible.

 const getRandomElement = arr => { const rand = Math.floor(Math.random() * arr.length); return arr[rand]; } const generateRandomPasswordSelection = (length) => { const uppercase = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']; const lowercase = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']; const special = ['~', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+', '-', '=', '{', '}', '[', ']', ':', ';', '?', ', ', '.', '|', '\\']; const numbers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']; const nonSpecial = [...uppercase, ...lowercase, ...numbers]; let password = ''; for (let i = 0; i < length; i++) { // Previous character is a special character if (i !== 0 && special.includes(password[i - 1])) { password += getRandomElement(nonSpecial); } else password += getRandomElement([...nonSpecial, ...special]); } return password; } const password = generateRandomPasswordSelection(16); console.log(password);

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!