What characters are allowed in an email address?

后端 未结 17 2475
天命终不由人
天命终不由人 2020-11-22 00:29

I\'m not asking about full email validation.

I just want to know what are allowed characters in user-name and server parts of email address

17条回答
  •  南方客
    南方客 (楼主)
    2020-11-22 01:17

    The accepted answer refers to a Wikipedia article when discussing the valid local-part of an email address, but Wikipedia is not an authority on this.

    IETF RFC 3696 is an authority on this matter, and should be consulted at section 3. Restrictions on email addresses on page 5:

    Contemporary email addresses consist of a "local part" separated from a "domain part" (a fully-qualified domain name) by an at-sign ("@"). The syntax of the domain part corresponds to that in the previous section. The concerns identified in that section about filtering and lists of names apply to the domain names used in an email context as well. The domain name can also be replaced by an IP address in square brackets, but that form is strongly discouraged except for testing and troubleshooting purposes.

    The local part may appear using the quoting conventions described below. The quoted forms are rarely used in practice, but are required for some legitimate purposes. Hence, they should not be rejected in filtering routines but, should instead be passed to the email system for evaluation by the destination host.

    The exact rule 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. For example

      Abc\@def@example.com
    

    is a valid form of an email address. Blank spaces may also appear, as in

      Fred\ Bloggs@example.com
    

    The backslash character may also be used to quote itself, e.g.,

      Joe.\\Blow@example.com
    

    In addition to quoting using the backslash character, conventional double-quote characters may be used to surround strings. For example

      "Abc@def"@example.com
    
      "Fred Bloggs"@example.com
    

    are alternate forms of the first two examples above. These quoted forms are rarely recommended, and are uncommon in practice, but, as discussed above, must be supported by applications that are processing email addresses. In particular, the quoted forms often appear in the context of addresses associated with transitions from other systems and contexts; those transitional requirements do still arise and, since a system that accepts a user-provided email address cannot "know" whether that address is associated with a legacy system, the address forms must be accepted and passed into the email environment.

    Without quotes, local-parts may consist of any combination of
    alphabetic characters, digits, or any of the special characters

      ! # $ % & ' * + - / = ?  ^ _ ` . { | } ~
    

    period (".") may also appear, but may not be used to start or end the local part, nor may two or more consecutive periods appear. Stated differently, any ASCII graphic (printing) character other than the at-sign ("@"), backslash, double quote, comma, or square brackets may appear without quoting. If any of that list of excluded characters are to appear, they must be quoted. Forms such as

      user+mailbox@example.com
    
      customer/department=shipping@example.com
    
      $A12345@example.com
    
      !def!xyz%abc@example.com
    
      _somename@example.com
    

    are valid and are seen fairly regularly, but any of the characters listed above are permitted.

    As others have done, I submit a regex that works for both PHP and JavaScript to validate email addresses:

    /^[a-z0-9!'#$%&*+\/=?^_`{|}~-]+(?:\.[a-z0-9!'#$%&*+\/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-zA-Z]{2,}$/i
    

提交回复
热议问题