我有一個如下的函數,它創建一個強密碼,但它並不總是添加一個特殊字符和一個數字或大寫字母給它,我如何確保它始終具有這些包含在它的要求。使用javascript創建強密碼
密碼要求很長,它已經
8個或更多字符,並且必須有一個特殊的字符, 大寫字母,其中包括一個編號。
我已經把現在顯示以下功能
function GenPwd(nMinimumLength,bIsRequired,sNameoftheButton){
var end=0;
var index=0;
var sPassCharactersWithoutSpeacil="!#[email protected]*123456789aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ";
var sSpecialCharacters="!#[email protected]*";
var nSrngLen=sStrgStrAddOnChrs
var sNumbers="123456789";
var nMinimunLen=Math.round(nBaseLen/2);
var PassWordDLength=Math.round((Math.random()*nMinLen)+nMinimumLength+nMinimumLength); //Make sure it is atleadt 8 characters long
var Password="";
var PasswordTemporary="";
for(index = 1; index <= PassWordDLength; index++) {
nCharacter = Math.round(Math.random()*65);
PasswordTemporary = sPassCharactersWithoutSpeacil.charAt(nCharacter);
while (PWD.indexOf(PasswordTemporary) > -1) {
nCharacter = '';
nCharacter = Math.round(Math.random()*65);
PasswordTemporary = sPassCharactersWithoutSpeacil.charAt(nChar);
}
FinalPWD = Password + PasswordTemporary;
}
這是我必須使用
function GenPwd(nBaseLen,bIsStrong,sBtnName){
var end=0;
var index=0;
var sPassStr="!#[email protected]*123456789aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ";
var sStrgStrAddOnChrs="!#[email protected]*";
var nSrngLen=sStrgStrAddOnChrs
var sStrgStrAddOnNum="123456789";
var nMinLen=Math.round(nBaseLen/2);
var PWDLen=Math.round((Math.random()*nMinLen)+nBaseLen);
var PWD="";
var PWDTmp="";
//Generate the password
for(index = 1; index <= PWDLen; index++) {
nChar = Math.round(Math.random()*65);
PWDTmp = sPassStr.charAt(nChar);
while (PWD.indexOf(PWDTmp) > -1) {
nChar = '';
nChar = Math.round(Math.random()*65);
PWDTmp = sPassStr.charAt(nChar);
}
PWD = PWD + PWDTmp;
}
document.getElementById("pwd").value=PWD;
EnableBtn(sBtnName);
return true;
}
爲什麼不創建一個GUID然後追加 – bryanmac