Name validation regex for email header

时间:2016-07-11 21:10:19

标签: php regex email security email-headers

I have a contact form, user supplies name and last name in separate fields. Server side it is used to form From header, like

From: $name $lastname <mailer@mydomain.com>

I have seen a lot of suggestions to use something like A-Za-z, but there are a lot of names containing characters outside latin alphabet. What would be the regex to allow foreign alphabet names, while keeping it secure from email injection?

1 个答案:

答案 0 :(得分:1)

  1. Some people have only one name, such as Stack Overflow user sai (https://s.ai/iaq)

  2. Many Hispanic people have multiple surnames, like Juan Pablo Fernández de Calderón García-Iglesias

  3. Some given names are compound words, such as "Billy Bob"

  4. Some surnames are compound words, such as "Vaughn Williams"

  5. Some names are very short, like Po Ng or Malcolm X

  6. Some people have symbols as names, such as The Artist (formerly known as Prince)

  7. Some names include dashes, or apostrophes

  8. Some people have unique names (that won't appear in lists of names), like D'Brickashaw Ferguson

So, the regex would be /.+/

If you're trying to prevent email addresses as names, I would find a good email address validator and make sure a name does not contain an email address