0

I'm trying to work on a regular expression I've found and make it work with my requirements. I have already modified the regular expression slightly to suit some of my requirements. However, I need help to implement some of the conditions. The email is divided into 3 parts:

  • the local part (before the last @);
  • the last @ symbol; and
  • the domain part (after the @).

The regular expression that I have so far is:

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

The local part of the email address shall only contains the following special characters: . " ( ) , : ; < > @ [ \ ].

An . in the local part of the email address should not be the first or last characters of the local part.

The condition for ( ) , : ; < > @ [ ] to be displayed in the local part of an email address is ( ) , : ; < > @ [ ] shall be contained between quotation marks.

In the local part of the email address, " and \ shall be preceded with a backslash (i.e. \" or \).

Each part of the domain shall be separated by a period, and the domain part of an email shall be less than 256 characters.

Jon Adams
  • 24,464
  • 18
  • 82
  • 120
Rohan More
  • 131
  • 3
  • 12
  • Have you done any research before starting building your own? – Mohsen Jun 11 '13 at 04:57
  • 6
    Yet another email regex! – devnull Jun 11 '13 at 04:58
  • 4
    I wouldn't trust complex email regexes, most of them will surely leave some email out, IMO best regex is `/[^@]+@[^@]/` – elclanrs Jun 11 '13 at 04:59
  • Yes I have used the below mentioned regex for reference and modified it to suit my requirements var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; – Rohan More Jun 11 '13 at 04:59
  • I think you just need to take that complex RE and replace `.+` with the character set that you allow between quotes. – Barmar Jun 11 '13 at 05:02
  • 1
    @elclanrs What you say is so true. In fact, valid emails can even contain more than one @ sign! (The domain comes after the last @ sign, not after the only @ sign.) For instance, "()<>[]:,;@\\\"!#$%&'*+-/=?^_`{}| ~.a"@example.org is a valid email! – Joseph Myers Jun 11 '13 at 05:02
  • 1
    @Joseph Myers: actually elclanrs has a valid point - regex may give false positive results, but not return false negatives ever. So that you could make slower but the most accurate check with MTA – zerkms Jun 11 '13 at 05:33
  • Updated re /^([a-zA-Z0-9!#$%&\\'*+\-\/=?\^_`{\|}~\\".]*\\"[a-zA-Z0-9!#$%&\\'*+\-\/=?^_`{\|}~;,:<>()@\[\].]*\\"[a-zA-Z0-9!#$%&\\'*+\-\/=?^_`{\|}~\\".]*|[a-zA-Z0-9!#$%&\\'*+\-\/=?^_`{\|}~\\".]*\\'[a-zA-Z0-9!#$%&\\'*+\-\/=?^_`{\|}~;,:<>()@\[\].]*\\'[a-zA-Z0-9!#$%&\\'*+\-\/=?^_`{\|}~\\".]*|[a-zA-Z0-9!#$%&\\'*+\-\/=?^_`{\|}~\\".]{1,64})@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z0-9]{1,}))$/; – Rohan More Dec 06 '13 at 07:12

0 Answers0