Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex-check if whole of string is a single repeating character - by PHP [duplicate]

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.

like image 993
dartaplace Avatar asked Dec 07 '25 09:12

dartaplace


1 Answers

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.

like image 62
pavel Avatar answered Dec 08 '25 23:12

pavel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!