fonction generate_password dans alternc.js

This commit is contained in:
quenenni 2017-08-17 04:35:51 +02:00
parent 756993878b
commit 956f6fc2c6
1 changed files with 42 additions and 15 deletions

View File

@ -59,24 +59,51 @@ function false_if_empty(id,err_msg) {
} }
} }
function generate_password(len){ function generate_password(passwordLength, classcount) {
len = parseInt(len); passwordLength = parseInt(passwordLength);
if(!len) if(!passwordLength)
len = 8; passwordLength = 8;
var password = "";
var chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; classcount = parseInt(classcount);
var charsN = chars.length; if(!classcount)
var nextChar; classcount = 3;
for(i=0; i<len; i++){ var numberChars = "0123456789";
nextChar = chars.charAt(Math.floor(Math.random()*charsN)); var upperChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
password += nextChar; var lowerChars = "abcdefghiklmnopqrstuvwxyz";
var specialchars = "(!#$%&'()*+,-./:;<=>?@[\]^_";
if (classcount >= 4) {
var allChars = numberChars + upperChars + lowerChars + specialchars;
} else {
var allChars = numberChars + upperChars + lowerChars;
} }
return password; var randPasswordArray = Array(passwordLength);
randPasswordArray[0] = numberChars;
randPasswordArray[1] = upperChars;
randPasswordArray[2] = lowerChars;
if (classcount == 4) {
randPasswordArray[3] = specialchars;
randPasswordArray = randPasswordArray.fill(allChars, 4);
} else {
randPasswordArray = randPasswordArray.fill(allChars, 3);
}
return shuffleArray(randPasswordArray.map(function(x) { return x[Math.floor(Math.random() * x.length)] })).join('');
} }
function generate_password_html(id, size, field1, field2) { function shuffleArray(array) {
$("#z"+id).html("<input id='inp"+id+"' type='textbox' size=8 readonly='readonly' value='"+generate_password(size)+"' />&nbsp;<a href='javascript:generate_password_html("+id+","+size+",\""+field1+"\",\""+field2+"\");'><img src='/images/refresh.png' alt='Refresh' title='Refresh'/></a>"); for (var i = array.length - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
var temp = array[i];
array[i] = array[j];
array[j] = temp;
}
return array;
}
function generate_password_html(id, size, field1, field2, classcount) {
$("#z"+id).html("<input id='inp"+id+"' type='textbox' size=8 readonly='readonly' value='"+generate_password(size, classcount)+"' />&nbsp;<a href='javascript:generate_password_html("+id+","+size+",\""+field1+"\",\""+field2+"\");'><img src='/images/refresh.png' alt='Refresh' title='Refresh'/></a>");
$("#inp"+id).focus(); $("#inp"+id).focus();
$("#inp"+id).select(); $("#inp"+id).select();
if (field1 != "") { $(field1).val( $("#inp"+id).val() ); } if (field1 != "") { $(field1).val( $("#inp"+id).val() ); }