Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex email - how to allow plus symbols in email?

I always find regular expressions a headache, and googling didn't really help. I'm 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'd want to allow emails with plus symbols, this obviously won't work, eg: [email protected]

How would I need to change my expression to allow it? Thanks in advance for all the help!

like image 822
Sam Granger Avatar asked Dec 03 '11 17:12

Sam Granger


People also ask

Can email be validated with regex?

Regex provides the ability to validate the structure of an email address. It can be handled with one or two lines of code and can easily be tweaked to handle a wide variation of different parameters. However, one thing to keep in mind is that it can only check the structure of an email address.

How do I make an email pattern in regex?

[a-zA-Z0-9+_. -] matches one character from the English alphabet (both cases), digits, “+”, “_”, “.” and, “-” before the @ symbol. + indicates the repetition of the above-mentioned set of characters one or more times. @ matches itself.

What is the best email regex?

The best all-around regex to find valid email addressesallows Latin characters ("a" - "z" or "A" - "Z") within the email address. permits digits (0 - 9) in the email address. enforces domain part restrictions. allows hyphens (-) inside the domain as long as they don't lead or trail the domain.


1 Answers

You should just use PHPs builtin regex for email validation, because it covers all the things:

filter_var($email, FILTER_VALIDATE_EMAIL)

See filter_var and FILTER_VALIDATE_EMAIL (or https://github.com/php/php-src/blob/master/ext/filter/logical_filters.c#L499 for the actual beast).

like image 82
mario Avatar answered Sep 28 '22 01:09

mario