ilSavo
ilSavo

Reputation: 852

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:

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:

More examples (updated):

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

Upvotes: 2

Views: 2086

Answers (1)

georg
georg

Reputation: 215009

^(?!.*?(.+)\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.

Upvotes: 3

Related Questions