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;
    };
  • 相关阅读:
    Server2012R2 ADFS3.0 The same client browser session has made '6' requests in the last '13'seconds
    pig的grunt中shell命令不稳定,能不用尽量不用
    pig脚本的参数传入,多个参数传入
    pig的cogroup详解
    pig的limit无效(返回所有记录)sample有效
    Dynamics CRM2013 任务列表添加自定义按钮
    简述浏览器渲染机制
    如何区分浏览器类型
    使用mock.js生成前端测试数据
    理解Ajax
  • 原文地址:https://www.cnblogs.com/huxiuxiu/p/14385546.html
Copyright © 2011-2022 走看看