2013-07-04 59 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