-8

我能够在 C# 中做到这一点,但无法将其转换为 Javascript。我找到了这篇文章“ Generate random password string with requirements in javascript ”,但我无法根据我的要求对其进行自定义:

密码长度应至少为 8 个字符,最多 13 个字符,并且必须包含以下每个字符串集合中的至少一个字符:

string specialCharacters = "~!@#$%^&*()_+=-|\\}]{[\"':;?/>.<,";
string numbers = "0123456789";
string smallLetters = "abcdefghijklmnopqrstuvwxyz";
string capitalLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

你能帮忙吗?非常感谢!

编辑:

这是我在 C# 中的代码。抱歉,有点长:

private string CreateRandomPassword(int passwordLength)
        {
            string specialCharacters = "~!@#$%^&*()_+=-|\\}]{[\"':;?/>.<,";
            string numbers = "0123456789";
            string smallLetters = "abcdefghijklmnopqrstuvwxyz";
            string capitalLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
            string allowedChars = "";

            char[] chars = new char[passwordLength];
            string password = String.Empty;
            Random rd = new Random();

            int ctr = 0;
            int prop = 4;

            if(!_password.ContainsSpecialCharacters && !_password.ContainsNumbers && !_password.ContainsSmallLetters && !_password.ContainsCapitalLetters)
                return String.Empty;

            string sc = "";
            string num = "";
            string sl = "";
            string cl = "";

            if(_password.ContainsSpecialCharacters)
            {
                // Get a special character randomly
                rd = new Random();
                sc = specialCharacters[rd.Next(0, specialCharacters.Length)].ToString();
                allowedChars += specialCharacters;
            }
            else
            {
                prop--;
            }

            if(_password.ContainsNumbers)
            {
                // Get a random number randomly
                rd = new Random();
                num = numbers[rd.Next(0, numbers.Length)].ToString();
                allowedChars += numbers;
            }
            else
            {
                prop--;
            }

            if(_password.ContainsSmallLetters)
            {
                // Get a small letter randomly
                rd = new Random();
                sl = smallLetters[rd.Next(0, smallLetters.Length)].ToString();
                allowedChars += smallLetters;
            }
            else
            {
                prop--;
            }

            if(_password.ContainsCapitalLetters)
            {
                // Get a capital letter randomly
                rd = new Random();
                cl = capitalLetters[rd.Next(0, capitalLetters.Length)].ToString();
                allowedChars += capitalLetters;

            }
            else
            {
                prop--;
            }

            for (; ctr < passwordLength - prop; ctr++)
                password += allowedChars[rd.Next(0, allowedChars.Length)];


            return password + sc + num + sl + cl;
        }
4

3 回答 3

0

这是我想出的。

只需在 JavaScript 控制台中运行它。

(function() {
    var passwd, classTypes, classCount, len, chars, classIndex, charIndex;
    do {
        passwd = "";
        classTypes = [];
        classCount = 0;
        len = Math.round(Math.random() * 5 + 8);
        chars = ["~!@#$%^&*()_+=-|\\}]{[\"':;?/>.<,", "0123456789",
            "abcdefghijklmnopqrstuvwxyz", "ABCDEFGHIJKLMNOPQRSTUVWXYZ"];
        while (passwd.length < len) {
            classIndex = Math.round(Math.random() * 3);
            classTypes[classIndex] = true;
            charIndex = Math.round(Math.random()
                * (chars[classIndex].length - 1));
            passwd += chars[classIndex].charAt(charIndex);
        }
        console.log(JSON.stringify(classTypes));
        classCount = classTypes.filter(function(value, index, object) {
            return value === true;
        }).length;
        console.log("classCount = " + classCount);
        console.log("passwd.length = " + passwd.length);
    } while (classCount < 4);
    console.log("generated pasword is \n" + passwd + "\n");
    console.log("but see http://xkcd.com/936/ for a better approach to paswords");
})();
于 2013-05-16T14:34:14.897 回答
-1

尝试

var specialCharacters = "~!@#$%^&*()_+=-|\\}]{[\"':;?/>.<,";
var numbers = "0123456789";
var smallLetters = "abcdefghijklmnopqrstuvwxyz";
var capitalLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

var srcs = [specialCharacters, numbers, smallLetters, capitalLetters];

function getRandomChar(string){
    var pos = Math.floor(Math.random() * string.length);
    return string.charAt(pos);
}

//+ Jonas Raoni Soares Silva
//@ http://jsfromhell.com/array/shuffle [v1.0]
function shuffle(o){ //v1.0
    for(var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
    return o;
}
function generate(){
    var len = 8 + Math.floor(Math.random() * 5);

    var array = [];
    array.push(getRandomChar(specialCharacters));
    array.push(getRandomChar(numbers));
    array.push(getRandomChar(smallLetters));
    array.push(getRandomChar(capitalLetters));

    for(var i = 4; i < len; i++){
        var pos = Math.floor(Math.random() * srcs.length);
        array.push(getRandomChar(srcs[pos]));
    }
    return shuffle(array).join('')
}

for(var i = 0; i < 10; i++){
    console.log(generate())
}

演示:小提琴

于 2013-05-16T12:51:47.577 回答
-1

要检查它是否包含任一字符串中的字符,您可以使用Javascript 正则表达式。只需根据链接将您的候选密码与这些字符匹配即可。你将尝试四场比赛。字符串的长度,可以查看它的length属性。

于 2013-05-16T12:40:45.263 回答