密碼強度正則,密碼規範驗證

公司的一點業務需求,本身研究了一下正則表達式

const checkArr = [
    {
        regexName: 'regexNum',
        value: /\d/
    },
    {
        regexName: 'regexLower',
        value: /[a-z]/
    },
    {
        regexName: 'regexUpper',
        value: /[A-Z]/
    },
    {
        regexName: 'regexSpeChar',
        value: /[~!@#$%^&*]/
    }
]
function checkPasswordLevel(password) {
    let count = 0;
    checkArr.forEach(item => {
        if(item.value.test(password)) {
            count++;
        }
    });
    switch (count) {
        case 4:
            console.log("密碼強度極高");
            break;
        case 3:
            console.log("密碼強度高");
            break;
        case 2:
            console.log("密碼強度中等");    
            break;
        case 1: 
            console.log("密碼強度低");    
            break;
        default:
            console.log("密碼不符合規定");    
            break;
    }
}
checkPasswordLevel("123");      // 密碼強度低
checkPasswordLevel("123as");    // 密碼強度中等
checkPasswordLevel("123asQW");  // 密碼強度高
checkPasswordLevel("123asQW!@");// 密碼強度極高
checkPasswordLevel("{}()");     // 密碼不符合規定

附加一些密碼驗證的正則表達式:code

密碼(以字母開頭,長度在6~18之間,只能包含字母、數字和下劃線):it

regex = /^[a-zA-Z]\w{5,17}$/

強密碼(必須包含大小寫字母和數字的組合,不能使用特殊字符,長度在8-16之間):io

regex = /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])[a-zA-Z0-9]{8,16}$/

強密碼(必須包含大小寫字母和數字的組合,可使用特殊字符,長度在8-16之間):console

regex = /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,16}$/

強密碼(必須包含大小寫字母和數字的組合,可使用特殊字符(~!@#$%^&*),長度在8-16之間):function

regex = /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])[a-zA-Z0-9~!@#$%^&*]{8,16}$/
相關文章
相關標籤/搜索