Raw Syntax

The stuff programs are made of

Rails 3 Email Validation


Most email validation I've encountered goes bananas with regards to regexes. The programmer tries to strictly adhere to the spec (rfc 822) in a regex. Not only is this task difficult and error prone, but there are functional email addresses out there that do not conform to the spec. Not to mention the resulting regex often looks like this.

When I do email validation I use a simple regex:

Which I took from the Devise codebase. It means:

  • begins with one or more word characters, period, percent, plus, or dashes
  • then one at symbol
  • then one or more word characters, dashes, and then a period
  • ends with 2 or more word characters
  • case-insensitive

This regex ensures a reasonably close to valid email address, without enumerating and worrying about .com vs .co.uk vs .musem and a host of other issues.

At this point, I have the application send an email to the address, and require the user to click a confirmation link. That's good enough, and very simple to implement.

Try this regex out on Rubular it's an interactive web based ruby regex engine.