Regular Expression for Password Strength Validation

后端 未结 6 1251
隐瞒了意图╮
隐瞒了意图╮ 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:02

    my example

    JS/jQuery

    $( "#password" ).change(function ()
    {
        strongPassword();
    });
    
    /**
     * @author websky
     */
    function strongPassword() {
        var p = document.getElementById('password').value;
        var label1 = 0; 
        var label2 = 0; 
        var label3 = 0; 
        var label4 = 0; 
        var label5 = 0;
        if (p.length > 6) {//min length
            label1 = 1;
        }
        if (/[!@#$%^&*?_~]{2,}/.test(p)) {//min 2 special characters
            label2 = 1;
        }
        if (/[a-z]{2,}/.test(p)) {//min 2 a-z
            label3 = 1;
        }
        if (/[A-Z]{2,}/.test(p)) {//min 2 A-Z
            label4 = 1;
        }
        if (/[0-9]{2,}/.test(p)) {//min 2 0-9
            label5 = 1; 
        }
    
        var strong_password = label1 + label2 + label3 + label4 + label5;
    
        if(strong_password > 0){
            //Here your action
            //
            //var progressbar = strong_password * 20; 
            //$( "#progressbar" ).progressbar({value: progressbar}); <== I use jQuery progessbar
        } 
    }
    

提交回复
热议问题