php preg_match无效

时间:2017-01-29 07:40:54

标签: php preg-match

这是我的注册页面。我在变量中调用了正则表达式代码,具体取决于允许的字符,不允许在文本框中。

      $txtlastname = $_POST["txtlastname"];
      $txtfirstname = $_POST["txtfirstname"];
      $txtaddress = $_POST["txtaddress"];
      $txtuser = $_POST["txtuser"];
      $txtpass = md5 ($_POST["txtpass"]);
      $txtcpass = md5 ($_POST["txtcpass"]);
      $txtbday = $_POST["txtbday"];
      $txtemail = $_POST["txtemail"];
      $txtcompany = $_POST["txtcompany"];

      $name = "/^[A-Z][a-zA-Z ]+$/";
      $emailValidation = "/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9]+(\.[a-z]{2,4})$/";
      $number = "/^[0-9]+$/";

这是我的if语句。我的问题是,即使我在文本框中输入正确的格式,然后单击提交。 “无效”消息仍在弹出。

if(!preg_match($name,$txtlastname)){
echo "$txtlastname is not valid";
exit();
}
if(!preg_match($name,$txtfirstname)){
echo "$txtfirstname is not valid";
exit();
}
if(!preg_match($emailValidation,$txtemail)){
echo "This $txtemail is not valid";
exit();
}

1 个答案:

答案 0 :(得分:0)

尝试一下-您没有添加iui开关。

<?php

 $txtlastname = $_POST["txtlastname"];
      $txtfirstname = $_POST["txtfirstname"];
      $txtaddress = $_POST["txtaddress"];
      $txtuser = $_POST["txtuser"];
      $txtpass = md5 ($_POST["txtpass"]);
      $txtcpass = md5 ($_POST["txtcpass"]);
      $txtbday = $_POST["txtbday"];
      $txtemail = $_POST["txtemail"];
      $txtcompany = $_POST["txtcompany"];

      $name = "/^[A-Z][a-zA-Z ]+$/iu";
      $emailValidation = "/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9]+(\.[a-z]{2,4})$/iu";
      $number = "/^[0-9]+$/iu";


if(!preg_match($name,$txtlastname)){
echo "$txtlastname is not valid";
exit();
}
if(!preg_match($name,$txtfirstname)){
echo "$txtfirstname is not valid";
exit();
}
if(!preg_match($emailValidation,$txtemail)){
echo "This $txtemail is not valid";
exit();
}

?>