2016-04-15 91 views
-4
extract($_POST); 

    // determine whether phone number is valid and print 
    // an error message if not 
    if ([email protected]("^[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 ([email protected]_match('/^(?=.*\d{3,})(?=.*[A-Za-z]{5,})[[email protected]#$%]{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>"); 
    } 

我不確定上面的代碼有什麼問題,但我似乎無法得到正確的密碼值?它持續顯示相同的錯誤消息正則表達式PHP與密碼長度爲8,至少有一個大寫字母,數字nd字母

+4

'extract($ _ POST)'是非常危險的,你一定會用你的代碼破解你的服務器。 –

+0

看看答案是否適合你 – rock321987

回答

0

如果你後正好 8個字符,這些條件,那麼你就需要這樣的事情,而不是:

else if ([email protected]_match('/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[a-zA-Z\d]{8}$/', $pass)) 
0

你的正則表達式的手段?

^(?=.*\d{3,})(?=.*[A-Za-z]{5,})[[email protected]#$%]{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 
    [[email protected]#$%]{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