Php preg_match with email authentication question

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

 preg_match(): No ending delimiter '^' found 

here is my template for preg_match

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

How to fix it?

+4
source share
7 answers

Just use:

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

Possible use

 filter_var($email, FILTER_VALIDATE_EMAIL); 

There would be a simpler approach.

+7
source

use this code

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

Using the filter_var () function in PHP would be easier if the postmaster wanted to allow RFC style matching for email addresses. For some applications or MTAs (sendmail, etc.) This may not be desirable. However, if you need to go along the preg_match () route, I would suggest exploring non-greedy quantifiers and grab instructions that don't use buffers. A good place to start would be http://us3.php.net/manual/en/book.pcre.php .

0
source

Due to problems caused by FILTER_VALIDATE_EMAIL (for example, this does not work well with non-Latin characters), I prefer to use:

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

0
source

This is a simple regular expression email validation method:

 public function emailValidation($email) { $regex = "/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[az]{2,10})$/"; $email = strtolower($email); return preg_match ($regex, $email); } 
0
source
  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 identifier of the input tag

-2
source

Source: https://habr.com/ru/post/1446863/


All Articles