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?

1

9 Answers 9

29

Just use:

$pattern = "/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,})$/i";
Sign up to request clarification or add additional context in comments.

4 Comments

This is not fully valid, because you can send email to [email protected] and your pattern would say this address is invalid. Just happened to me in combination with MS Exchange which creates such email addresses.
@mkudlacek You want to lowercase the email before applying the pattern.
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 Fair enough. I've added the i at the end.
16

Maybe using

filter_var($email, FILTER_VALIDATE_EMAIL);

Would be an easier approach.

1 Comment

Be careful of using filter_var on email with IDN domains! For example such working emails: путин@президент.рф or 用户@例子.广告 will be treated as incorrect. You will need idn_to_ascii() function.
8

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";
?>

1 Comment

This doesn't match domains like .agency.
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);

Comments

1

Updated the pattern to also allow addresses like [email protected]

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

Comments

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 https://www.php.net/manual/en/book.pcre.php .

Comments

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

Comments

0

Just add "^" after $ at the end :

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

Comments

-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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.