Regex to validate password strength

后端 未结 11 953
天涯浪人
天涯浪人 2020-11-22 02:13

My password strength criteria is as below :

  • 8 characters length
  • 2 letters in Upper Case
  • 1 Special Character (!@#$&*)
  • <
11条回答
  •  醉梦人生
    2020-11-22 02:49

    All of above regex unfortunately didn't worked for me. A strong password's basic rules are

    • Should contain at least a capital letter
    • Should contain at least a small letter
    • Should contain at least a number
    • Should contain at least a special character
    • And minimum length

    So, Best Regex would be

    ^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[!@#\$%\^&\*]).{8,}$
    

    The above regex have minimum length of 8. You can change it from {8,} to {any_number,}

    Modification in rules?

    let' say you want minimum x characters small letters, y characters capital letters, z characters numbers, Total minimum length w. Then try below regex

    ^(?=.*[a-z]{x,})(?=.*[A-Z]{y,})(?=.*[0-9]{z,})(?=.*[!@#\$%\^&\*]).{w,}$
    

    Note: Change x, y, z, w in regex

    Edit: Updated regex answer

    Edit2: Added modification

提交回复
热议问题