Generate random password string with requirements in javascript

前端 未结 20 1763
夕颜
夕颜 2020-12-07 07:56

I want to generate a random string that has to have 5 letters from a-z and 3 numbers.

How can I do this with JavaScript?

I\'ve got the following script, but

20条回答
  •  清歌不尽
    2020-12-07 08:14

    And finally, without using floating point hacks:

    function genpasswd(n) {
        // 36 ** 11 > Number.MAX_SAFE_INTEGER
        if (n > 10)
            throw new Error('Too big n for this function');
        var x = "0000000000" + Math.floor(Number.MAX_SAFE_INTEGER * Math.random()).toString(36);
        return x.slice(-n);
    }
    

提交回复
热议问题