Creating a strong password using javascript

I have a function, as shown below, that creates a strong password, however it does not always add a special character, number or capital letter to it, how can I ensure that these requirements are always included in it.

Password requirements are that it must

8 or more characters and must have a special character, an uppercase letter and a number included in it.

now the function i now displays below

function GenPwd(nMinimumLength,bIsRequired,sNameoftheButton){
            var end=0;
            var index=0;
            var sPassCharactersWithoutSpeacil="!#$@*123456789aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ";
            var sSpecialCharacters="!#$@*";
            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;
            }

here is what i should use

function GenPwd(nBaseLen,bIsStrong,sBtnName){

        var end=0;
        var index=0;
        var sPassStr="!#$@*123456789aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ";
        var sStrgStrAddOnChrs="!#$@*";
        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;
    }
+4
source share
1 answer

You can create a function that ensures that your pwd contains at least one of a specific character set:

function ensurePwdContains(pwd, regexListOfChars, listOfChars) {
    if (!regexListOfChars.test(pwd)) {
        // select random char from listOfChars
        var newChar = listOfChars.charAt(Math.floor(Math.random() * listOfChars.length));

        // insert it into the current pwd in a random location
        // must do +1 to include the location at the end of the string
        var pos = Math.floor(Math.random() * (pwd.length + 1));
        pwd = pwd.slice(0, pos) + newChar + pwd.slice(pos);
    }
    return pwd;
}

, . , listOfChars, .

:

FinalPwd = ensurePwdContains(FinalPwd, /[!#\$@*]/, sSpecialCharacters);
FinalPwd = ensurePwdContains(FinalPwd, /[A-Z]/, "ABCDEFGHIJKLMNOPQRSTUWXYZ");

: http://jsfiddle.net/jfriend00/ur9FL/


. , , , ( , ). :

function generatePwd(minLen) {
    // create a pwd that is between minLen and ((2 * minLen) + 2) long
    // don't repeat any characters
    // require at least one special char and one capital char
    function rand(max) {
        return Math.floor(Math.random() * max);
    }

    function ensurePwdContains(pwd, regexListOfChars, listOfChars) {
        if (!regexListOfChars.test(pwd)) {
            var newChar = listOfChars.charAt(rand(listOfChars.length));
            var pos = rand(pwd.length + 1);
            pwd = pwd.slice(0, pos) + newChar + pwd.slice(pos);
        }
        return pwd;
    }

    var legalChars = "!#$@*123456789aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ";
    var specialChars = "!#$@*";
    var specialRegex = /[!#\$@*]/;
    var caps = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    var capsRegex = /[A-Z]/;
    var nums = "123456789";
    var numRegex = /[1-9]/;

    // make legalChars into an array of chars (easier to deal with)
    legalChars = legalChars.split("");
    var pwd = "", len = minLen + rand(minLen), index;

    // ensure len is not too long
    len = Math.min(legalChars.length, len);

    // now add chars to the pwd string until it is of the desired length
    while (pwd.length < len) {
        index = rand(legalChars.length);
        pwd += legalChars[index];
        // remove the char we just used
        legalChars.splice(index, 1);
    }
    // ensure we have at least one special character and one caps char and one 1-9
    pwd = ensurePwdContains(pwd, specialRegex, specialChars);
    pwd = ensurePwdContains(pwd, capsRegex, caps);
    pwd = ensurePwdContains(pwd, numRegex, nums);
    return pwd;    
}

, , , , : http://jsfiddle.net/jfriend00/7dReY/ p >


, , :

function putPasswordInPlace(pwdId, sBtnName) {
     var pwd = generatePwd(8);
     document.getElementById(pwdId).value = pwd;
     EnableButton(sBtnName);
}
+3

All Articles