Regex for checking that at least 3 of 4 different character groups exist

后端 未结 6 661
一个人的身影
一个人的身影 2020-12-01 13:04

I\'m trying to write a password validator.

How can I see if my supplied string contains at least 3 different character groups?

It\'s easy enough to check if

6条回答
  •  遥遥无期
    2020-12-01 13:24

    I am assuming that you will be using different regexes for different requirements. In that case, tell me if the following work for you:

    var e = password.match(/.{8,}/); //At least 8 chars
    
    var a = password.match(/[0-9]+/); //numeric
    var b = password.match(/[A-Z]+/); //Capitals
    var c = password.match(/[a-z]+/); //small letters
    var d = password.match(/[!@#\$%&/=?_.,:;-\\]+/); //special chars
    
    if (a + b + c + d > 2 && e) {// Success}
    else {// Failure}
    

提交回复
热议问题