February 15th, 2012, 10:50 PM
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 firstname.lastname@example.org and email@example.com, so I need something which will work in those formats, as well as any other format. Thanks in advance.
February 16th, 2012, 12:52 AM
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. "firstname.lastname@example.org" is a syntactically valid E-Mail address that is trivial to type and is still totally useless to you.
February 16th, 2012, 12:28 PM
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.
February 17th, 2012, 01:53 AM
...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.
had to add / before and after the regex in order for it to work
Wishing you all a beautiful day.