Blog ComercialWeb - São Paulo - SP
E-mail: [email protected]
Atualizado: 06/10/2025

Gerador de senha completo em JavaScript — com cópia e critérios

Gerador de senha em JavaScript

Interface HTML

<div id="app">
  <input type="number" id="len" value="12" min="6" max="64"> tamanho
  <label><input type="checkbox" id="up" checked> Maiúsculas</label>
  <label><input type="checkbox" id="low" checked> Minúsculas</label>
  <label><input type="checkbox" id="num" checked> Números</label>
  <label><input type="checkbox" id="sym"> Símbolos</label>
  <button id="gen">Gerar</button> <button id="copy">Copiar</button>
  <input type="text" id="out" readonly>
</div>

JavaScript

<script>
const up = document.getElementById('up'), low = document.getElementById('low'), num = document.getElementById('num'), sym = document.getElementById('sym'), len = document.getElementById('len'), out = document.getElementById('out');
const A = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', a = 'abcdefghijklmnopqrstuvwxyz', d = '0123456789', s = '!@#$%&*_-+=';
function rand(str){ return str[Math.floor(Math.random()*str.length)]; }
document.getElementById('gen').addEventListener('click', () => { let pool = ''; if(up.checked) pool += A; if(low.checked) pool += a; if(num.checked) pool += d; if(sym.checked) pool += s; if(!pool) { out.value = 'Selecione ao menos um critério'; return; } let pass = ''; for(let i=0; i < Number(len.value); i++) pass += rand(pool); out.value = pass; });
document.getElementById('copy').addEventListener('click', async () => { try { await navigator.clipboard.writeText(out.value); out.value += ' ✔'; } catch(e) { out.select(); document.execCommand('copy'); } }); </script>

Dica: evite senhas repetidas em serviços diferentes e prefira armazenar em um gerenciador de senhas confiável.

Fonte: Redação


Mais Lidos