2017-07-07 91 views
0

林試圖讓用戶驗證一個的preg_match規則的preg_match用戶驗證

只允許AZ - AZ - 0-9 和人物_- = @:?!, 沒有空格

我已經嘗試了很多組合,但似乎沒有人上班

這是林試圖讓:

if(preg_match('idk what to use here', '[email protected]?')) { 
     return true; 
    } 

    if(preg_match('idk what to use here', '$Hello')) { 
     return false; 
    } 

    if(preg_match('idk what to use here', 'Hello 123')) { 
     return false; 
    } 

任何人都知道這個正則表達式? 謝謝:)

回答

0

使用一個類,但一定要逃脫連字符或把它作爲最後一個字符,否則它表示一個範圍。使用^$要求所有輸入的字符匹配的模式:

$regex = "/^[[email protected]:.,-]*$/"; 

var_dump(preg_match($regex, '[email protected]?')); 
var_dump(preg_match($regex, '$Hello')); 
var_dump(preg_match($regex, 'Hello 123')); 
+0

由於做工精良:) –