Are email addresses allowed to contain non-alphanu

2020-01-27 03:20发布

I'm building a website using Django. The website could have a significant number of users from non-English speaking countries.

I just want to know if there are any technical restrictions on what types of characters an email address could contain.

Are email addresses only allowed to contain English letters, numbers, _, @ and .?

Are they allowed to contain non-English alphabets like é or ü?

Are they allowed to contain Chinese or Japanese or other Unicode characters?

7条回答
迷人小祖宗
2楼-- · 2020-01-27 03:32

Email address consists of two parts local before @ and domain that goes after.

Rules to these parts are different:

For local part you can use ASCII:

  • Latin letters A - Z a - z
  • digits 0 - 9
  • special characters !#$%&'*+-/=?^_`{|}~
  • dot ., that it is not first or last, and not in sequence
  • space and "(),:;<>@[] characters are allowed with restrictions (they are only allowed inside a quoted string, a backslash or double-quote must be preceded by a backslash)
  • Plus since 2012 you can use international characters above U+007F, encoded as UTF-8.

Domain part is more restricted:

  • Latin letters A - Z a - z
  • digits 0 - 9
  • hyphen -, that is not first or last, multiple hyphens in sequence are allowed.

Regex to validate

^(([^<>()\[\]\.,;:\s@\"]+(\.[^<>()\[\]\.,;:\s@\"]+)*)|(\".+\"))@(([^<>()[\]\.,;:\s@\"]+\.)+[^<>()[\]\.,;:\s@\"]{2,})

Hope this saves you some time.

查看更多
Rolldiameter
3楼-- · 2020-01-27 03:36

Instead of worrying about what email addresses can and can't contain, which you really don't care about, test whether your setup can send them email or not—this is what you really care about! This means actually sending a verification email.

Otherwise, you can't catch a much more common case of accidental typos that stay within any character set you devise. (Quick: is random@mydomain.com a valid address for me to use at your site, or not?) It also avoids unnecessarily and gratuitously alienating any users when you tell them their perfectly valid and correct address is wrong. You still may not be able to process some addresses (this is necessary alienation), as the other answers say: email address processing isn't trivial; but that's something they need to find out if they want to provide you with an email address!

All you should check is that the user supplies some text before an @, some text after it, and the address isn't outrageously long (say 1000 characters). If you want to provide a warning ("this looks like trouble! is there a typo? double-check before continuing"), that's fine, but it shouldn't block the add-email-address process.

Of course, if you don't care to ever send email to them, then just take whatever they enter. For example, the address might solely be used for Gravatar, but Gravatar verifies all email addresses anyway.

查看更多
欢心
4楼-- · 2020-01-27 03:39

Well, yes. Read (at least) this article from Wikipedia.

I live in Argentina and here are allowed emails like ñoñó1234@server.com

查看更多
姐就是有狂的资本
5楼-- · 2020-01-27 03:49

There is a possibility to have non-ASCII email addresses, as shown by this RFC: http://tools.ietf.org/html/rfc3490 but I think this has not been set for all countries, and from what I understand only one language code will be allowed for each country, and there is also a way to turn it into ASCII, but that won't be a trivial issue.

查看更多
smile是对你的礼貌
6楼-- · 2020-01-27 03:50

The allowed syntax in an email address is described in RFC 3696, and is pretty involved.

The exact rule [for local part; the part before the '@'] is that any ASCII character, including control characters, may appear quoted, or in a quoted string. When quoting is needed, the backslash character is used to quote the following character
[...]
Without quotes, local-parts may consist of any combination of alphabetic characters, digits, or any of the special characters ! # $ % & ' * + - / = ? ^ _ ` . { | } ~
[...]
Any characters, or combination of bits (as octets), are permitted in DNS names. However, there is a preferred form that is required by most applications...

...and so on, in some depth.

查看更多
家丑人穷心不美
7楼-- · 2020-01-27 03:52

Building on @Matas Vaitkevicius' answer: I've fixed up the regex some more in Python, to have it match valid email addresses as defined on this page and this page of wikipedia, using that awesome regex101 website: https://regex101.com/r/uP2oL7/26

Hope this helps someone!:)

查看更多
登录 后发表回答