Skip to content

Instantly share code, notes, and snippets.

@vjt
Created March 29, 2012 16:52
Show Gist options
  • Save vjt/2239787 to your computer and use it in GitHub Desktop.
Save vjt/2239787 to your computer and use it in GitHub Desktop.
Javascript Random Password Generator (jQuery)
// Demo: http://jsbin.com/emisib/5/edit#preview
//
// Markup:
//
// <input type="text" name="password" id="password" />
// <a href="#" class="passworder" data-length="10" data-target="#password">Generate</a>
//
// - [email protected] - public domain
//
(function () {
var rand = function (str) {
return str[Math.floor (Math.random () * str.length)];
};
var get = function (source, len, a) {
for (var i = 0; i < len; i++)
a.push (rand (source));
return a;
};
var alpha = function (len, a) {
return get ("A1BCD2EFG3HIJ4KLM5NOP6QRS7TUV8WXY9Z", len, a);
};
var symbol = function (len, a) {
return get ("-:;_$!", len, a);
};
$('.passworder').live ('click', function (event) {
event.preventDefault ();
var widget = $(this);
var target = $(widget.data ('target'));
var length = widget.data ('length') - 1;
// Alpha{ceil((length-1)/2))} Symbol Alpha{floor((length-1)/(2))}
var l = Math.floor (length/2), r = Math.ceil (length/2);
var ret = alpha (l, symbol (1, alpha (r, []))).join('');
target.val (ret);
});
}) ();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment