#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2011
    Posts
    9
    Rep Power
    0

    An email address regex which will work with any format


    Could someone please help me create a regex for a validation in a WordPress form? The regex should be for any email address. I had a regex which didn't work for email addresses in any format, such as blue.gum@hotmail.com and bluegum1980@domainname.com, so I need something which will work in those formats, as well as any other format. Thanks in advance.
  2. #2
  3. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,316
    Rep Power
    7171
    Code:
    ^.+@.+\..+$
    This will match every valid E-Mail address except those belonging to top level domains (ex: root@localhost). However, in most cases on a web form you don't want to receive a top level E-Mail address anyway.

    It will also match a lot of invalid E-Mail addresses, but that doesn't matter for the following reasons:

    (1) This regex is sufficient to catch a vast majority of catch-able accidental E-Mail formatting errors.

    (2) You won't find a regex that can validate all E-Mail address formats with 100% accuracy.

    (3) Complex regex's of the type that can validate the address format of most E-Mail addresses are expensive to parse and run.

    (4) If a user intentionally does not want to provide you with their E-Mail address, a regex will not force them to do so, no matter how complex it is. "no@site.com" is a syntactically valid E-Mail address that is trivial to type and is still totally useless to you.
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2011
    Posts
    9
    Rep Power
    0
    Thanks, I have used your regex, but had to add / before and after the regex in order for it to work. The email address field works now.
  6. #4
  7. Turn left at the third duck
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2011
    Location
    Nelson, NZ
    Posts
    112
    Rep Power
    94
    Code:
    had to add / before and after the regex in order for it to work
    ...and probably other code specific to your language. Just for reference, the slashes (/) are not part of the regex. They are delimiters and language-specific, so what E-Oreo gave you (what you used) is known as "the regex". With the slashes or tildes or whatever delimiter, maybe you'd call that the "delimited regex string" or "delimited pattern". It is very common on this forum to share regexes in this "pure form" because we don't know if you'll used a naked regex (eg in RegexBuddy or a text editor) or if you'll need to dress it up for your language.
    Wishing you all a beautiful day.

IMN logo majestic logo threadwatch logo seochat tools logo