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!
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.
[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.
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.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With