7

I am using this expression to validate e-mail addresses:

"^[_a-z0-9-]+(.[a-z0-9-]+)@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,4})$"

I noticed that in order for the e-mail address to validate, I need to put in at least 3 characters before the @ symbol. Is this a requirement? What if I just want to have an e-mail address with 1 or 2 characters before the @ symbol?

Xaisoft
  • 45,655
  • 87
  • 279
  • 432
  • I found this here. I am not sure how to change it to allow a minimum of 1 character. – Xaisoft Jul 19 '11 at 17:31
  • where did you find this on SO? This REGEX you provide is wrong, for many reasons. – Brad Jul 19 '11 at 17:32
  • MS Outlook doesn't prevent you from using `a@b.com`. – agent-j Jul 19 '11 at 17:32
  • @Brad - I found it here: http://stackoverflow.com/questions/369543/validating-e-mail-with-regular-expression-vb-net/369554#369554 – Xaisoft Jul 19 '11 at 17:35
  • Basically I just need to check if the user is entering something like a@a.com or a@a.au or a@a.com2 – Xaisoft Jul 19 '11 at 17:35
  • I find all regex solutions to the problem of validating the structure of an e-mail address to be woefully inadequate. I have quite a number of examples of what some might argue are "edge cases" that are 100% valid. Some forms of notation (comments within `()` and target folder selection via `+` notation) are incredibly useful, but too often artificially restricted because of use of bad regexen such as several demonstrated here. Instead, split and validate the parts. It's a structured value. (Edited to note: but w/o arbitrary limits such as TLD length restrictions.) – amcgregor Nov 17 '21 at 17:12

4 Answers4

8

It is entirely possible to have an e-mail address with just one character. All of the guidelines are defined in RFC822: http://www.ietf.org/rfc/rfc0822.txt?number=822

The necessary REGEX: http://www.ex-parrot.com/pdw/Mail-RFC822-Address.html

Brad
  • 159,648
  • 54
  • 349
  • 530
  • How would I change my regular expression to reflect that? – Xaisoft Jul 19 '11 at 17:29
  • Yes, that regex is way too long. The one I have is fine, I just want to allow a minimum of 1 character before the @. – Xaisoft Jul 19 '11 at 17:31
  • 2
    @Xaisoft, the one you have is **not** fine, it is incorrect. Suppose I want to use `#` in my e-mail address. – Brad Jul 19 '11 at 17:33
  • I found it here: http://stackoverflow.com/questions/369543/validating-e-mail-with-regular-expression-vb-net/369554#369554. I don't need to cover all bases. – Xaisoft Jul 19 '11 at 17:34
  • 1
    @Brad: it isn't _perfect_, but I think it's fine--I doubt that a non-negligible amount of people uses a "#" in their email address. – Tikhon Jelvis Jul 19 '11 at 17:35
  • 2
    @Tikhon, your doubts are wrong. I know several people who use `#` to designate different tags and such in their mailbox. It is a basic feature that is easy to allow via regex. There is little excuse for not doing so. – Brad Jul 19 '11 at 17:37
  • @Brad: good point. I have never heard of using `#` to do that, but I have seen people use `+`. – Tikhon Jelvis Jul 19 '11 at 18:07
  • Apparently, it's even possible to have a email address with 0 characters in front of it. Something like `""@example.com`. This is equivalent to having no local part and is valid. See: http://tools.ietf.org/html/rfc5321#section-4.5.3.1.3 – conradkleinespel Aug 16 '14 at 09:31
2

0, if you don’t count quoting double-quotes. If you do, the minimum’s 1.

""@example.com

a@example.com


RFC 5321

   Mailbox        = Local-part "@" ( Domain / address-literal )

   Local-part     = Dot-string / Quoted-string
                  ; MAY be case-sensitive


   Dot-string     = Atom *("."  Atom)

   Atom           = 1*atext

   Quoted-string  = DQUOTE *QcontentSMTP DQUOTE
Community
  • 1
  • 1
1

Your regex is bad. You really should change it. Even Microsoft offers more flexible variant:

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

Yes, it is long. But you should write good programs, not fast developed programs.

VMAtm
  • 27,943
  • 17
  • 79
  • 125
-2

I went ahead and decided to use this one:

"^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$"

It allows me to enter 1 or more characters for the local part.

Xaisoft
  • 45,655
  • 87
  • 279
  • 432
  • An explanation of why this was downvoted would be nice. This is one I found on here, I believe it is from regular-expressions-info. – Xaisoft Jul 19 '11 at 19:13
  • What do you mean by quality in this case? If it is because it does not match certain addresses, I would not define that as quality. I would say that is more of a quantitative aspect to it. The regular expression I am looking for does not need to validate every possible address in the world. If it just had a @ symbol that would be fine. – Xaisoft Jul 19 '11 at 19:33
  • 1
    This criteria is bad criteria. That's why downvotes, I think. – VMAtm Jul 19 '11 at 19:51