jquery-password-generator-plugin
Version:
PassGen Password Generator jQuery Plugin
50 lines (43 loc) • 1.23 kB
JavaScript
/*
* Copyright (c) 2015 Sergey Sokurenko
* Licensed under the MIT license.
*
*/
(function ($) {
$.passGen = function (options) {
// Override default options with passed-in options
options = $.extend({}, $.passGen.options, options);
// Local varialbles declaration
var charsets, charset = '', password = '', index;
// Available character lists
charsets = {
'numeric' : '0123456789',
'lowercase' : 'abcdefghijklmnopqrstuvwxyz',
'uppercase' : 'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
'special' : '~!@#$%^&*()-+[]{}<>?'
};
// Defining merged character set
$.each(charsets, function(key, value) {
if (options[key]) {
charset += value;
}
});
// Generating the password
for (var i=0; i< options.length; i++) {
// defining random character index
index = Math.floor(Math.random() * (charset.length));
// adding the character to the password
password += charset[index];
}
// Returning generated password value
return password;
};
// Default options
$.passGen.options = {
'length' : 10,
'numeric' : true,
'lowercase' : true,
'uppercase' : true,
'special' : false
};
}(jQuery));