我正在尝试创建一个用于用户名、密码和电子邮件验证的简单regex检查器。
要求如下:
用户名:
- Only letters and numbers (a-z, A-z, 0-9).
- No spaces, linebreaks, tabs or special characters.
- At least 5 characters in length.
- No more than 20 characters in length.
密码:
- At least 8 digits in length.
- no more than 40 characters in length.
- Must contain at least 1 special character or number.
电子邮件:
- Must follow the format "value@value.com".
这就是我目前所拥有的:
用户名-正在工作
$username = $enteredUsername;
if(preg_match('/^[a-zA-Z0-9]{5,30}+$/', $username) == false ) {
echo "the username is invalid";
}
密码:尚未工作:
$password = $enteredPassword;
if(preg_match('/^[a-zA-Z0-9]+['w0-9]{8,}+$/', $password) == false ) {
echo "the password is invalid";
}
电子邮件-正在工作
$email = $enetered email.
if (filter_var($email, FILTER_VALIDATE_EMAIL) == false) {
echo "the email is invalid";
}
电子邮件和用户名有效,但我似乎无法找到正确的密码正则表达式。
对于密码regex,请尝试:
^(?=.*[^a-zA-Z]).{8,40}$
这使用了排除字母字符的正向前瞻,以确保包含特殊字符或数字。