简体   繁体   中英

Regular expression - avoid the repetition of the sequence of the same letters

I'm trying to make a check on the password inserted by a user, working on a PHP website. My check wants to:

  • at least 8 characters
  • maximum 20 characters
  • accept letters, numbers, and common special characters like (dot) @ + $ - _ !

Until this point I've been able to figure out the right expression, but now I want to add an other rule, where an user can't write the same sequence of letter more then 1 time.

Let's say that, not considering the repetition of two times of the same letter, if the user write the same string (equal or more than 3 characters) more then once, it should not match.

For example:

  • abcde not valid - should be at least 8 characters
  • abcde1234 valid
  • abcd1abcd1 not valid due to repetition of the string abcd1

More examples (updated):

  • abababab not valid - the string "ab" is repited 2 times or more
  • aaaaaaaa not valid - the string aaa is repited more then once
  • helloworld valid - even if there is the letter "l" repeated two times

Any suggestion?
I don't know is it's possibile to write down a correct RegExp, maybe I'm trying to do something impossibile.
Before leaving the idea, I was curious to check the opinion of someone who know more then me in RegExp.
Thanks in advance

^(?!.*?(.+)\1)([\w@+$!.-]+){8,20}$

seems to work well: http://regex101.com/r/cU9lD0/1

The tricky part is ^(?!.*?(.+)\\1) which reads "start of input, when not followed by: something, then some substring, then that substring once again".

That being said, all "password validation" is a quite pointless enterprise, which actually stops people from using really good passwords .

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