正则表达式 PHP,密码长度为 8,至少 1 个大写字母,数字 nd 字母


Regex PHP with password length of 8 with at least 1 upper case, number nd letter

extract( $_POST );
    // determine whether phone number is valid and print
    // an error message if not
    if ( !@ereg( "^[0-9]{3}-[0-9]{7}$", $phone))
    {
        echo( "<SCRIPT LANGUAGE='JavaScript'>
        window.alert('Please insert a valid phone number with (xxx-xxxxxxx) format.')
        window.location.href='';
        </SCRIPT>");
    }
else if (!@preg_match('/^(?=.*'d{3,})(?=.*[A-Za-z]{5,})[0-9A-Za-z!@#$%]{8,32}$/', $pass))
    {
        echo("<SCRIPT LANGUAGE='JavaScript'> 
        window.alert('Password must be at least 8 characters long and must contain at least 1 number and 1 letter')
        window.location.href='';
        </SCRIPT>");
    }

我不确定上面的编码有什么问题,但我似乎无法获得正确的密码值?它一直显示相同的错误消息

如果你在这些条件下正追求 8 个字符,那么你将需要这样的东西:

else if (!@preg_match('/^(?=.*[a-z])(?=.*[A-Z])(?=.*'d)[a-zA-Z'd]{8}$/', $pass))

你的正则表达式是什么意思?

^(?=.*'d{3,})(?=.*[A-Za-z]{5,})[0-9A-Za-z!@#$%]{8,32}$

正则表达式细分

^ #Start of string
  (?=.*'d{3,}) #Match at least 3 consecutive digits anywhere in string
  (?=.*[A-Za-z]{5,}) #Match at least 5 consecutive characters from the set anywhere in string
  [0-9A-Za-z!@#$%]{8,32} #Ensure that length of string is in between 8 and 32
$ #End of string

这将给出您想要的(根据您在问题中的描述(

(?=.{8,})(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])

正则表达式细分

(?=.{8,}) #Ensure that string is of at least length 8 
(?=.*[A-Z]) #Match a single capital letter anywhere in string
(?=.*[a-z]) #Match a single small letter anywhere in string
(?=.*'d) #Match a single digit anywhere in string