Validating email addresses with Regular Expressions

Posted Tue, 23 Jan 2007

Wrote a quick function today also to verify that the syntax of an email address is valid, using regular expressions.

^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$

This will match a userid containing letters, numbers, underscores, dots and hyphens, followed by an at sign, then a domain name containing letters, numbers, hyphens and dots, followed by a suffix of 2-to-4 characters.

Using this regex, I can ensure that the user has entered a potentially valid email address before attempting to send out a confirmation email.

Related Books

Web Analytics: An Hour a Day Mastering Regular Expressions The Ruby Programming Language Cocoa(R) Programming for Mac(R) OS X (3rd Edition) The Complete Guide to E-mail Marketing: How to Create Successful, Spam-free Campaigns to Reach Your Target Audience and Increase Sales

Comments

On June 22, 2008 at 07:15 PM Timothy Fanelli wrote:

Zachary - Thanks for the links - I skimmed the first briefly and will take a look at the second over the weekend, and maybe update my plugin before it gets released.

On June 22, 2008 at 07:15 PM Les Hazlewood wrote:

Hi Tim,

In the spirit of this blog post, I thought I'd forward on mine, which implements RFC2822, which supercedes the old RFC822. It is in java, but should be easily translatable to Ruby:

http://www.leshazlewood.com/?p=5

Cheers,

Les

On June 22, 2008 at 07:15 PM Zachary Bedell wrote:

That's a good start, and it'll probably cover the vast majority of cases, but... To truly check for all valid email address possibilities, you've got a lot more work to do.

See:

http://www.regular-expressions.info/email.html

http://www.ex-parrot.com/~pdw/Mail-RFC822-Address.html

On June 22, 2008 at 07:15 PM Les Hazlewood wrote:

Hi Tim,

In the spirit of this blog post, I thought I'd forward on mine, which implements RFC2822, which supercedes the old RFC822. It is in java, but should be easily translatable to Ruby:

http://www.leshazlewood.com/?p=5

Cheers,

Les

Post a Comment




About

My name is Tim Fanelli, I am a software engineer in Northern NY. I spend most of my time working, and when I can, I try to post interesting things here.

Cigar Dossiers