Reputation: 411
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!
Upvotes: 5
Views: 5629
Reputation: 145482
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).
Upvotes: 10
Reputation: 26930
Your wrong regex can be changed to another wrong regex:
/^[\w-]+(\.[\w+-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/
which allows for the +
character where you want it. But it's wrong anyway.
Upvotes: 7
Reputation: 1258
Try add \+
into the char collection []
:
/^[_a-z0-9-]+(.[_a-z0-9-\+]+)@[a-z0-9-]+(.[a-z0-9-]+)(.[a-z]{2,3})$/
Upvotes: 1