2012-08-08 104 views
8

我想創建一個preg_match函數來驗證我的密碼,但我不確定如何編寫它以允許使用以下特殊字符:[email protected]#$%

if(!preg_match(?????)$/', $password)) 

下面是我要工作到正則表達式我的密碼規則:

  • 可能含有字母和數字
  • 必須至少包含1個數字和1個字母
  • 可以包含任何的這些字符:[email protected]#$%
  • 必須是8-12個字符

感謝您提供任何幫助。

+2

請考慮允許更長的密碼,我更喜歡我的是20 +字符長,它不會傷害你,讓這個。我不明白具有最大長度的網站 - 您將它們存儲爲散列,那麼問題是什麼? – 2012-08-08 22:24:27

+0

@Dragon我把最多可以增加到50個字符。這是一個簡單的網站,我想我只是想讓8-12個字符讓人們更容易記住,而不太可能需要重置密碼。但正如你所說,密碼被加密後,這真的不是什麼大問題。 – 2012-08-09 00:36:40

+0

我讓lastpass記住我的所有密碼:-) – 2012-08-09 02:34:20

回答

41

我想這應該看起來像:

if(!preg_match('/^(?=.*\d)(?=.*[A-Za-z])[[email protected]#$%]{8,12}$/', $password)) { 
    echo 'the password does not meet the requirements!'; 
} 

開始之間 - >^
而結束 - >$
字符串的 必須有至少一個數字 - >(?=.*\d)
和至少一個字母 - >(?=.*[A-Za-z])
它必須是數字,字母或以下其中一個:!@#$% - >[[email protected]#$%]
還有必須在8-12個字符 - >{8,12}

至於user557846評論你的問題,我也建議你,讓更多的人物,我通常(如果我使用最多)至少需要50 :)

順便說一句,你可能想看看this regex tutorial

+3

謝謝!這對我有用。並感謝你解釋它的每一部分。這真的幫助我更好地理解這一點。我還探討了你建議的教程,這看起來像是一個很好的資源,下次我正在處理正則表達式。我製作了最多50個字符。 – 2012-08-09 00:33:08

-1

查找字符類,正則表達式的基本功能。

+0

謝謝你指引我朝着正確的方向。 – 2012-08-09 00:34:00

+0

這是邊界不是答案,特別是比較接受的答案。也許你可以擴展它,而不是隻是告訴別人去尋找某些東西。謝謝。 – Kev 2012-08-09 21:42:15

-1
if(!preg_match('/^(?=.*\d)(?=.*[A-Za-z])[[email protected]#$%]{8,12}$/', $password)) { 
    echo 'the password does not meet the requirements!'; 
    } 

在上面的語句..什麼可能的口令從而存在?

+0

請幫助我..因爲我輸入了不同的字符但它不存在..「密碼不符合要求! – christine 2014-03-17 06:01:06

+0

if(!preg_match('/ ^(?=。* \ d)(?=。* [0-9A-Za-z!@#$%] {8,12} $ /',$ password)){ echo'密碼不符合要求!'; } – christine 2014-03-17 07:24:01

+0

在上面的語句...是任何機構可以給我一個適合我的「preg_match密碼」的密碼..因爲我輸入dffernt密碼組合,但它不能提交.. – christine 2014-03-17 07:27:23

6
preg_match('/^(?=.*\d)(?=.*[@#\-_$%^&+=§!\?])(?=.*[a-z])(?=.*[A-Z])[[email protected]#\-_$%^&+=§!\?]{8,20}$/',$password) 
  • 至少一個小寫字符
  • 至少一個大寫字符
  • 至少一個數字
  • 至少一個@#$ -_特殊標誌%^ & + =§! ?
+0

托馬斯的答案比r3bel的更好地解釋。 – Alreadytakenindeed 2017-12-17 02:23:04

0

我已經用我的drupal自定義表單在hook_form_validate這裏做了這個密碼應該是6個字母的字母,數字和至少一個特殊字符。

<? 
if (!preg_match('/^(?=.*\d)(?=.*[A-Za-z])(?=.*[[email protected]#$%])[[email protected]#$%]{6,15}$/', $form_state['values']['pass'])) { 
    form_set_error('pass', t('Password must contain 6 characters of letters, numbers and at least one special character.')); 
} 
?> 
1

我已經制定了一個稍微複雜一些檢查

/^(?=.*\d)(?=.*[A-Za-z])(?=.*[A-Z])(?=.*[a-z])(?=.*[ !#$%&'\(\) * +,-.\/[\\]^_`{|}~\"])[0-9A-Za-z !#$%&'\(\) * +,-.\/[\\]^_`{|}~\"]{8,50}$/ 

基本上我檢查密碼有1位,資本,1個和特殊字符,一個完整的正則表達式。我希望這可以幫助尋找正則表達式的人。

0
if (!preg_match('/^(?=.*\d)(?=.*[A-Za-z])(?=.*[[email protected]#$%])[[email protected]#$%] 
{6,15}$/',($_POST['password']))) { 
    $message='Password must contain 6 characters of letters, numbers and 
    at least one special character.'; 
} 
+0

你應該更清楚。 – 2017-01-11 07:44:40

2

我喜歡r3bel的答案,所以我有一個發揮與它結束了以下內容作爲密碼檢查功能:

function password_strength_check($password, $min_len = 8, $max_len = 70, $req_digit = 1, $req_lower = 1, $req_upper = 1, $req_symbol = 1) { 
    // Build regex string depending on requirements for the password 
    $regex = '/^'; 
    if ($req_digit == 1) { $regex .= '(?=.*\d)'; }    // Match at least 1 digit 
    if ($req_lower == 1) { $regex .= '(?=.*[a-z])'; }   // Match at least 1 lowercase letter 
    if ($req_upper == 1) { $regex .= '(?=.*[A-Z])'; }   // Match at least 1 uppercase letter 
    if ($req_symbol == 1) { $regex .= '(?=.*[^a-zA-Z\d])'; } // Match at least 1 character that is none of the above 
    $regex .= '.{' . $min_len . ',' . $max_len . '}$/'; 

    if(preg_match($regex, $password)) { 
     return TRUE; 
    } else { 
     return FALSE; 
    } 
} 

最大/最小長度是默認或可調,每一個需求是默認開啓的,但可以關閉,並且我想支持任何符號,所以最後的要求是「任何不屬於上述類型的東西」,而不是固定的一組符號。