Password validation regex

后端 未结 2 754
半阙折子戏
半阙折子戏 2020-11-27 07:15

I am trying to get one regular expression that does the following:

  1. makes sure there are no white-space characters
  2. minimum length of 8
  3. makes s
相关标签:
2条回答
  • 2020-11-27 07:58
    ^(?=.*[^a-zA-Z])(?=.*[a-z])(?=.*[A-Z])\S{8,}$
    

    should do. Be aware, though, that you're only validating ASCII letters. Is Ä not a letter for your requirements?

    \S means "any character except whitespace", so by using this instead of the dot, and by anchoring the regex at the start and end of the string, we make sure that the string doesn't contain any whitespace.

    I also removed the unnecessary parentheses around the entire expression.

    0 讨论(0)
  • 2020-11-27 08:09

    Tim's answer works well, and is a good reminder that there are many ways to solve the same problem with regexes, but you were on the right track to finding a solution yourself. If you had changed (?!\s) to (?!.*\s) and added the ^ and $ anchors to the end, it would work.

    ^((?=.*[^a-zA-Z])(?=.*[a-z])(?=.*[A-Z])(?!.*\s).{8,})$
    
    0 讨论(0)
提交回复
热议问题