function password(length, special) { var iteration = 0; var password = ""; var randomNumber; if(special == undefined){ var special = false; } while(iteration < length){ randomNumber = (Math.floor((Math.random() * 100)) % 94) + 33; if(!special){ if ((randomNumber >=33) && (randomNumber <=47)) { continue; } if ((randomNumber >=58) && (randomNumber <=64)) { continue; } if ((randomNumber >=91) && (randomNumber <=96)) { continue; } if ((randomNumber >=123) && (randomNumber <=126)) { continue; } } iteration++; password += String.fromCharCode(randomNumber); } return password; }
http://www.jb51.net/article/24534.htm
function randPassword() { var text=['abcdefghijklmnopqrstuvwxyz','ABCDEFGHIJKLMNOPQRSTUVWXYZ','1234567890','~!@#$%^&*()_+";",./?<>']; var rand = function(min, max){return Math.floor(Math.max(min, Math.random() * (max+1)));} var len = rand(8, 16); // 长度为8-16 var pw = ''; for(i=0; i<len; ++i) { var strpos = rand(0, 3); pw += text[strpos].charAt(rand(0, text[strpos].length)); } return pw; }