Regular Expression for Password Strength Validation

后端 未结 6 2168
眼角桃花
眼角桃花 2021-01-01 02:29

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 02:50

    You can do it by separating each group out and match them one by one, like this:

    var level = 0;
    var input = '';//user input goes here
    switch(true){
        case /^(?:([A-Z])*){8,12}$/.test(input):
        level = 1;
        break;
    
        case /^(?:([A-Z])*([a-z])*){8,12}$/.test(input):
        level = 2;
        break;
    
        case /^(?:([A-Z])*([a-z])*(\d)*){8,12}$/.test(input):
        level = 3;
        break;
    
        case /^(?:([A-Z])*([a-z])*(\d)*(\W)*){8,12}$/.test(input):
        level = 4;
        break;
    }
    

    The level variable goes from 1 (the weakest) to 4 (the strongest).

提交回复
热议问题