const UPPERCASE = 1; // 包含大写字母
LOWERCASE = 2; // 包含小写字母
NUMBER = 4; // 包含数字
let RandomString = (len, include) => {
len = len || 8; // 默认生成8位长度
include = include || 7; // 默认生成大小写数字混合
let uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
let lowercase = uppercase.toLowerCase();
let number = "0123456789";
let chars = "";
if (include & UPPERCASE) {
chars += uppercase;
}
if (include & LOWERCASE) {
chars += lowercase;
}
if (include & NUMBER) {
chars += number;
}
let maxPos = chars.length;
let str = "";
for (let i = 0; i < len; i++) {
str += chars.charAt(Math.floor(Math.random() * maxPos));
}
return str;
};
console.log(RandomString
js实现生成随机密码字符串,可自定义包含类型与位数。
最新推荐文章于 2024-07-25 13:55:35 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)