开发者

Having a problem validating email address in PHP with preg_match

开发者 https://www.devze.com 2023-02-21 14:45 出处:网络
Here\'s the code: if(trim($_POST[\'email\']) == \'\'){ $hasError = true; } els开发者_如何学Pythone if (!preg_match(\"/^[A-Z0-9._%-]+@[A-Z0-9._%-]+\\.[A-Z]{2,4}$\", trim($_POST[\'email\']))) {

Here's the code:

if(trim($_POST['email']) == '')  {
        $hasError = true;
    } els开发者_如何学Pythone if (!preg_match("/^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,4}$", trim($_POST['email']))) {
        $hasError = true;
    } else {
        $email = trim($_POST['email']);
    }

Here's the error msg:

Warning: preg_match() [function.preg-match]: No ending delimiter '/' found in /home/bigsilkd/public_html/UBA/join.php on line 22


It's exactly what it says:

preg_match("/^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,4}$"

should be

preg_match("/^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,4}$/"
                                                      ^
                                                      |
                                  This was missing ---/


You shouldn't use regular expressions for validating emails. For example your regex wouldn't allow my email address +@example.org, which is a normal and valid email. Save my email! It's dying out, because of bad form validation! Use filter_var!

if (filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
    // email is valid
}
0

精彩评论

暂无评论...
验证码 换一张
取 消