2013-07-04 83 views
0

我嘗試驗證網站的註冊表單。不幸的是,無論我在哪裏使用一個錯誤的條目,它顯示我的第一個狀態框:PHP:preg_match將無法正常工作

if(isset($_POST['send'])){ 
    $freshpw = generate_pw(); 
    $mdpassword = md5($freshpw); 
    $timestamp = mktime(0,0,0,$_POST['month'],$_POST['day'],$_POST['year']); 
    $reggebdatum = date("d.m.Y",$timestamp); 
    if (empty($_POST['regusername']) || check_username($_POST['regusername'])){ 
     statusbox("Username is given!", 0); 
     echo "<div class='sections'> 
       </div></div>"; 
     header("HTTP/1.1 301 Moved Permanently"); 
     header("refresh:1;url=index.php?act=register"); 
    } 

else if(preg_match("/^[A-Z][a-zA-Z -]+$/", $_POST['regname']) === 0){ 
     statusbox("Name can only contains letters spaces and dashes!",0); 
     echo "<div class='sections'> 
       </div></div>"; 
     header("HTTP/1.1 301 Moved Permanently"); 
     header("refresh:1;url=index.php?act=register"); 
     } 


     else if(preg_match("/^[A-Z][a-zA-Z -]+$/", $_POST['regstreet']) === 0){ 
       statusbox("Street can only contains letters spaces and dashes!",0); 

     echo "<div class='sections'> 
       </div></div>"; 
     header("HTTP/1.1 301 Moved Permanently"); 
     header("refresh:1;url=index.php?act=register"); 
     } 
... 

如果我測試它並寫出某事。目的地錯誤,像街道或郵編總是向我顯示錯誤的名稱:名稱只能包含字母空格和破折號

我是否應該這樣做?錯誤??

+0

你對它進行了什麼測試?你知道它必須以大寫字母開頭嗎?爲什麼'preg_match(...)=== 0'當你應該執行'!preg_match(...)',因爲它可能是'0'或'false'以防錯誤。 – CodeAngry

+0

你是什麼意思:「......它必須以大寫字母開頭?」 ?究竟在哪裏? 當我使用!preg_match(...)而不是=== 0不會意味着我需要更改preg match(反轉所有內容)的內部?不知道你的意思:/ – JOP

+0

'^ [A-Z]'這意味着以大寫字母「A-Z」開頭。 'if(!preg_match())...'表示如果(不匹配(pattern))...'。所以這是錯誤處理部分。爲了成功測試你做'if(preg_match())...'。 – CodeAngry

回答

0

你知道它必須以大寫字母開頭嗎?^[A-Z]這意味着以大寫字母:A-Z開頭。

爲什麼preg_match(...) === 0應該這樣做!preg_match(...),因爲它可能是0false萬一發生錯誤。 if(!preg_match())...意味着if(does not match(pattern) or pattern is broken)...所以這是錯誤處理部分。爲了成功測試你做if(preg_match())...

+0

好的謝謝。下次我會記住它。我會在接下來的幾天嘗試!preg_match(...)。 – JOP