Search code examples
phpregexvalidationpreg-matchemail-validation

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!


Solution

  • 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).