zoukankan      html  css  js  c++  java
  • 随机生成8-20位包含大小写字母、数字,特殊字符可有可无和正则校验

    const createPassword = (min, max) => {
        // 可以生成随机密码的相关数组
        const num = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
        const english = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k',
            'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'];
        const ENGLISH = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L',
            'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];
        const config = num.concat(english).concat(ENGLISH);
        // 随机从数组中抽出一个数值
        const getOne = arr => arr[Math.floor(Math.random() * arr.length)];
        // 先放入一个必须存在的
        const arr = [];
        arr.push(getOne(num));
        arr.push(getOne(english));
        arr.push(getOne(ENGLISH));
        // 获取需要生成的长度
        const len = min + Math.floor(Math.random() * (max - min + 1));
        for (let i = 4; i < len; i++) {
            // 从数组里面抽出一个
            arr.push(config[Math.floor(Math.random() * config.length)]);
        }
        // 乱序
        const newArr = [];
        for (let j = 0; j < len; j++) {
            newArr.push(arr.splice(Math.random() * arr.length, 1)[0]);
        }
        return newArr.join('');
    };
    // 生成随机密码
    const randomPassword = createPassword(8, 20);

    2.8~20 位同时包含数字和大小写字母,符号仅限 !@#$%^*()正则校验

    const checkPassword = password => {
        // 8~20 位同时包含数字和大小写字母,符号仅限 !@#$%^*()
        const pattern = /(?![0-9A-Z]+$)(?![0-9a-z]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,20}$/;
        if (pattern.test(password)
    || /(?![0-9A-Z!@#$%^*()]+$)(?![0-9a-z!@#$%^*()]+$)(?![a-zA-Z!@#$%^*()]+$)[0-9A-Za-z!@#$%^*()]{8,20}$/.test(password)) {
            return true;
        }
        return false;
    };
  • 相关阅读:
    Installshield Basic msi 禁用控制面板中添加删除应用
    js location.search
    c# 中关于office引用释放 技巧
    Installshield2016 condition
    bat script 点滴
    Installshield 2015 实现检测某安装文件是否存在并运行安装
    (八)基本概念列表
    (七)PM法律法规、文档配置、需求管理知识
    (六)PM项目沟通、风险、采购管理
    (五)PM项目质量管理与人力资源管理
  • 原文地址:https://www.cnblogs.com/huxiuxiu/p/14385546.html
Copyright © 2011-2022 走看看