Regular Expression for Password Strength Validation

后端 未结 6 1285
隐瞒了意图╮
隐瞒了意图╮ 2021-01-01 02:21

I have written a regular expression which could potentially be used for password strength validation:

^(?:([A-Z])*([a-z])*(\\d)*(\\W)*){8,12}$
6条回答
  •  孤独总比滥情好
    2021-01-01 03:16

    Here was my final solution based on sp00m's answer:

    function testPassword(pwString) {
        var strength = 0;
    
        strength += /[A-Z]+/.test(pwString) ? 1 : 0;
        strength += /[a-z]+/.test(pwString) ? 1 : 0;
        strength += /[0-9]+/.test(pwString) ? 1 : 0;
        strength += /[\W]+/.test(pwString) ? 1 : 0;
    
        switch(strength) {
            case 3:
                // its's medium!
                break;
            case 4:
                // it's strong!
                break;
            default:
                // it's weak!
                break;
        }
    }
    

    I've added this purely for reference, however have accepted sp00m's answer since it was their answer that let me to this solution.

提交回复
热议问题