Email Regex - how to enable plus characters in email?

I always find regular expressions a headache, and googling didn't really help. I am currently using the following expression (preg_match):/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/

However, if I want to allow emails with plus characters, this obviously will not work, for example: foo+bar@domain.com

How do I need to change my expression so that it can be done? Thank you in advance for your help!

+5
source share
3 answers

You just need to use PHP's built-in regular expressions to check your email, as it covers everything:

filter_var($email, FILTER_VALIDATE_EMAIL)

filter_var FILTER_VALIDATE_EMAIL ( https://github.com/php/php-src/blob/master/ext/filter/logical_filters.c#L499 ).

+8

:

/^[\w-]+(\.[\w+-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/

+, . .

+7

\+ char []:

/^[_a-z0-9-]+(.[_a-z0-9-\+]+)@[a-z0-9-]+(.[a-z0-9-]+)(.[a-z]{2,3})$/
+1

All Articles