PHP:preg_match无法正常工作

时间:2013-07-04 03:03:13

标签: php validation 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");
        }
...

如果我测试并写一下......错误的,如街道或Zip,它总是向我显示名称中的错误:名称只能包含字母空格和短划线

我是吗?错??

1 个答案:

答案 0 :(得分:0)

你知道它必须以大写字母开头吗? ^[A-Z]这意味着以大写字母开头:A-Z

为什么preg_match(...) === 0应该!preg_match(...),如果出现错误,可能会0falseif(!preg_match())...表示if(does not match(pattern) or pattern is broken)...所以这是错误处理部分。要进行成功测试,请执行if(preg_match())...