php – Login check preg_match

Question:

Login validation regex required.

It is allowed to use only Latin letters, numbers, "-", "_" (line and underscore). The minimum amount of lat. letters – 4, maximum number of numbers – 4, underscores and dashes – 4.

My original line:

if( preg_match( "/[\||\'|\<|\>|\[|\]|\"|\!|\?|\$|\@|\#|\/|\\\|\&\~\*\{\+]/", $name ) )
  $stop .= $lang['reg_err_4'];

How can you correct it to meet all the conditions?

Answer:

you can additionally introduce a limit on the number of characters, for example, from 6 to 12

/([A-Za-z\d\-_]){6,12}/

so that you can't have two underscores in a row

^([A-Za-z\d\-]|_(?!_)){6,12}$

so that there are no two underscores or minus in a row

^([A-Za-z\d]|_(?!_)|\-(?!\-)){6,12}$
Scroll to Top