Home  >  Article  >  Backend Development  >  PHP regular expression to verify username and password strength

PHP regular expression to verify username and password strength

王林
王林Original
2023-06-25 08:23:031144browse

In website development, username and password verification are a very important part of the design of user registration and login interfaces. In order to ensure the security and privacy of users, we need to strictly verify and restrict these input data. In this process, regular expressions are a very powerful tool for validating and limiting the strength of usernames and passwords.

PHP is a programming language that runs on the server side and has rich regular expression support. Let’s take a look at how to use regular expressions in PHP to verify the strength of usernames and passwords.

  1. Verify username

When verifying a username, we need to consider the following aspects:

  • Length: The length of the username Should be between 6-20 characters.
  • Character set: Username can only contain letters, numbers and underscores.
  • Start and end: The beginning and end of the username cannot be underscores.

Based on the above requirements, we can use the following regular expression to verify the user name:

/^[a-zA-Z0-9_]{6,20}$/ 

This regular expression means:

  • ^ represents the beginning of the string.
  • [] represents the character set, which contains letters, numbers and underscores.
  • {} indicates the limited length, which means the length is between 6-20.
  • $ represents the end of the string.

Use the preg_match() function to verify the user name. The sample code is as follows:

$pattern = '/^[a-zA-Z0-9_]{6,20}$/';
$username = "myUsername_123";
if (preg_match($pattern, $username)) {
    echo "用户名格式正确";
} else {
    echo "用户名格式不正确";
}
  1. Verify password

Verify password When doing so, we need to consider the following aspects:

  • Length: The length of the password should be between 8-16 characters.
  • Character set: Password can contain uppercase and lowercase letters, numbers and special characters.
  • Complexity: Password must contain at least one uppercase letter, one lowercase letter, one number and one special character.

Based on the above requirements, we can use the following regular expression to verify the password:

/^(?=.*[a-z])(?=.*[A-Z])(?=.*d)(?=.*[@$!%*?&])[A-Za-zd@$!%*?&]{8,16}$/

This regular expression means:

  • ^ Represents the beginning of the string.
  • (?=.*[a-z]) means it must contain at least one lowercase letter.
  • (?=.*[A-Z]) means it must contain at least one uppercase letter.
  • (?=.*d) means it must contain at least one number.
  • (?=.[@$!%?&]) means that it must contain at least one special character.
  • [A-Za-zd@$!%*?&] means it contains uppercase and lowercase letters, numbers and special characters.
  • {8,16} indicates that the password length is between 8-16 characters.
  • $ represents the end of the string.

Use the preg_match() function to verify passwords. The sample code is as follows:

$pattern = '/^(?=.*[a-z])(?=.*[A-Z])(?=.*d)(?=.*[@$!%*?&])[A-Za-zd@$!%*?&]{8,16}$/';
$password = "myPassword_123!";
if (preg_match($pattern, $password)) {
    echo "密码格式正确";
} else {
    echo "密码格式不正确";
}

Through the above example code, we can see the power of PHP regular expressions. Verification and restrictions on user names and passwords can be implemented conveniently and quickly. In actual website development, we can make targeted adjustments and optimizations according to specific needs and security requirements.

The above is the detailed content of PHP regular expression to verify username and password strength. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn