Skip to content

Instantly share code, notes, and snippets.

@Astargh
Created November 2, 2018 09:32
Show Gist options
  • Save Astargh/c3e3dde8e043a0bc1c6614a6d0477f09 to your computer and use it in GitHub Desktop.
Save Astargh/c3e3dde8e043a0bc1c6614a6d0477f09 to your computer and use it in GitHub Desktop.
Random password method
randomPassword(length) {
const chars = 'abcdefghijklmnopqrstuvwxyz!@#$%^&*()-+<>ABCDEFGHIJKLMNOP1234567890';
let pass = '';
for (let x = 0; x < length; x++) {
let i = Math.floor(Math.random() * chars.length);
pass += chars.charAt(i);
}
if (pass.match(/^(?=.*\d)(?=.*[^a-zA-Z\d\s:])(?=.*[a-z])(?=.*[A-Z])(?!.*\s).{8,15}$/g) === null) {
return this.randomPassword(length);
}
return pass;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment