10

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?

Om3ga
  • 30,465
  • 43
  • 141
  • 221
  • Related: [Using a regular expression to validate an email address](http://stackoverflow.com/questions/201323/using-a-regular-expression-to-validate-an-email-address?rq=1) – Dezza Sep 23 '16 at 07:24

9 Answers9

29

Just use:

$pattern = "/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,})$/i";
Shoe
  • 74,840
  • 36
  • 166
  • 272
  • 2
    This is not fully valid, because you can send email to Martin.Kudlacek@example.com and your pattern would say this address is invalid. Just happened to me in combination with MS Exchange which creates such email addresses. – mkudlacek Sep 05 '17 at 07:24
  • @mkudlacek You want to lowercase the email before applying the pattern. – Shoe Sep 05 '17 at 13:46
  • 1
    That's not an answer to the original question. The question is how to validate with preg_match(). The correct pattern would be: /^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*(\.[a-zA-Z]{2,})$/ or extended with /i for case insensitive matching. – mkudlacek Sep 07 '17 at 07:23
  • 1
    @mkudlacek Fair enough. I've added the `i` at the end. – Shoe Sep 08 '17 at 16:36
16

Maybe using

filter_var($email, FILTER_VALIDATE_EMAIL);

Would be an easier approach.

Peter O.
  • 32,158
  • 14
  • 82
  • 96
André Keller
  • 3,179
  • 3
  • 15
  • 23
  • 3
    Be careful of using filter_var on email with [IDN domains](https://en.wikipedia.org/wiki/International_email)! For example such working emails: путин@президент.рф or 用户@例子.广告 will be treated as incorrect. You will need idn_to_ascii() function. – Sergey Bogdanov Dec 24 '19 at 13:14
8

use this code

<?php
 $email = "asd/sdff@asdf.com"; 
 $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";
?>
manish1706
  • 1,571
  • 24
  • 22
1

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);

Pinonirvana
  • 920
  • 1
  • 8
  • 12
1

Updated the pattern to also allow addresses like name.surname+t3st@gmail.com

$pattern = "/^[_a-z0-9-+]+(\.[_a-z0-9-+]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,})$/i";
Imants
  • 11
  • 2
0

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 .

Anthony Rutledge
  • 6,980
  • 2
  • 39
  • 44
0

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);
}
muskose
  • 73
  • 1
  • 3
0

Just add "^" after $ at the end :

$pattern = "^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$^";
Henry Ecker
  • 34,399
  • 18
  • 41
  • 57
-2
    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

rOcKiNg RhO
  • 631
  • 1
  • 6
  • 16