简体   繁体   中英

Regex: Password validation of php

I'm working on some password validation on my new project. I have this rules that i want to convert on regex pattern using php preg_match() function:

  • Accept all characters.
  • Except spaces.
  • With minimum of 4 chars.
  • Max 20 chars.

Thanks you in advance!

Try this

(?s)^(\S{4,20})$

Explanation

"(?s)" +     // Match the remainder of the regex with the options: dot matches newline (s)
"^" +        // Assert position at the beginning of the string
"(" +        // Match the regular expression below and capture its match into backreference number 1
   "\\S" +       // Match a single character that is a “non-whitespace character”
      "{4,20}" +      // Between 4 and 20 times, as many times as possible, giving back as needed (greedy)
")" +
"$"          // Assert position at the end of the string (or before the line break at the end of the string, if any)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM