'php preg_match with email validation issue

I am validating email address using php with preg_match function. But I keep getting following error

preg_match(): No ending delimiter '^' found

here is my pattern for preg_match

$pattern = "^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$";

How to fix this?

php


Solution 1:[1]

Maybe using

filter_var($email, FILTER_VALIDATE_EMAIL);

Would be an easier approach.

Solution 2:[2]

use this code

<?php
 $email = "asd/[email protected]"; 
 $regex = '/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/'; 
 $email = (preg_match($regex, $email))?$email:"invalid email";
?>

Solution 3:[3]

Because of the issues caused by FILTER_VALIDATE_EMAIL (for instance it doesn't work well with not-latin characters), I prefer to use:

preg_match("/^[^@]+@[^@]+\.[a-z]{2,6}$/i",$email_address);

Solution 4:[4]

Using the function filter_var() in PHP would only be easier if a postmaster wanted to allow an RFC style match for e-mail addresses. For some applications or MTAs (sendmail, etc...), this might not be desirable. However, if one is to go the preg_match() route, I would suggest investigating non-greedy quantifiers and capture statements that do not use buffers. A good place to start would be http://us3.php.net/manual/en/book.pcre.php .

Solution 5:[5]

This is a simple email validation method with regex:

public function emailValidation($email) 
{
    $regex = "/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,10})$/";
    $email = strtolower($email);

    return preg_match ($regex, $email);
}

Solution 6:[6]

Just add "^" after $ at the end :

$pattern = "^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$^";

Solution 7:[7]

    var emailid = $.trim($('#emailid').val());  
  if( ! /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(emailid)){
                alert("<?php echo "email_invalid" ?>");

                return false;
            }

emailid is the id of the input tag

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Peter O.
Solution 2 manish1706
Solution 3 Pinonirvana
Solution 4 Anthony Rutledge
Solution 5 muskose
Solution 6 Henry Ecker
Solution 7 rOcKiNg RhO