2

I want to prevent users to just use repeated one letter or any others characters in whole string of username or password fields for registering new user by PHP. I search in google and regex wiki and stackoverflow and regex101 but I couldn't find my answer.

minimum of length for username and password characters is 8.

Rules:

in username or password fields for registering new user 11111111,111111111111,22222222,222222222222,...,aaaaaaaa,aaaaaaaaaaaa,bbbbbbbb,...,zzzzzzzz,...@@@@@@@@,******** ,&&&&&&&&... should be invalid but 11111113,22222221,...,aaaaaaab,baaaaaaa,...,zzzczzzz,...@@@1@@@@,2******* ,&&&&&n&&... should be valid.

I found this patterns but doesnt work and has warning:

 pattern1:    ^(?!.*(.)\1).{*}$
 pattern2:    ^(.)\1{1,}$

warning is this:

Warning: preg_match(): No ending delimiter '^' found in C:...\test.php on line 70

please help me to resolve that.

this posts was not my answer and doesnt work:

match the same unknown character multiple times

Perl Regex prevent repeated characters for password policy

UPDATE:

Thanks panter.it works==>~^(.)\1{1,}$~

Thanks solarc. Your suggestion is good.

dartaplace
  • 33
  • 4

2 Answers2

4

In PHP preg_* functions the patterns has to have delimiters around. Ofter used is ~, but it can be whatever.

~^(?!.*(.)\1).{*}$~
~^(.)\1{1,}$~

If you can work with the character which is used as a delimiter in your regex, you need to escape it.

pavel
  • 26,538
  • 10
  • 45
  • 61
4

Why regex?

You could simply use

if ($mystring === str_repeat($mystring[0], strlen($mystring))) {
    // invalid
}
solarc
  • 5,638
  • 2
  • 40
  • 51