Is there a rails way to validate format of an email address?

How can I validate and email address without using complex reg-ex is there a simple rails way to do this?

Rajinder Yadav wrote:

How can I validate and email address without using complex reg-ex is
there a simple rails way to do this?

Are you nuts? That's just the sort of thing Regexp was designed to do.
And, it's not really all that complex. The Rails docs even use email
validation as the example for validates_format_of so it's not like you
have to write the "complex" regex yourself. Just copy-and-paste:

  class Person < ActiveRecord::Base
    validates_format_of :email, :with =>
/\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i, :on => :create
  end

Robert Walker wrote:

Rajinder Yadav wrote:

How can I validate and email address without using complex reg-ex is
there a simple rails way to do this?

Are you nuts? That's just the sort of thing Regexp was designed to do.
And, it's not really all that complex.

Yes it is. The *only* correct regexps I am aware of for e-mail
addresses are on the order of a page in length. All shorter regexps
reject some valid e-mail addresses.

Just check for the . and @, and trust the user for the rest.

The Rails docs even use email
validation as the example for validates_format_of so it's not like you
have to write the "complex" regex yourself. Just copy-and-paste:

  class Person < ActiveRecord::Base
    validates_format_of :email, :with =>
/\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i, :on => :create
  end

That regexp is just a simple example. It isn't even close to covering
all valid e-mail addresses. Don't use it.

Best,

Marnen Laibow-Koser wrote:

Robert Walker wrote:

Rajinder Yadav wrote:

How can I validate and email address without using complex reg-ex is
there a simple rails way to do this?

Are you nuts? That's just the sort of thing Regexp was designed to do.
And, it's not really all that complex.

Yes it is. The *only* correct regexps I am aware of for e-mail
addresses are on the order of a page in length. All shorter regexps
reject some valid e-mail addresses.

Thanks Marnen. I stand corrected.

However, just to clarify, are you saying that Regexp is still the best
approach? Given that what you're saying is true, which I'm sure it is,
then I could imagine that performing this validation without using
Regexp would be even more complex and require quite a bit more code.

Robert Walker wrote:

Marnen Laibow-Koser wrote:

Robert Walker wrote:

Rajinder Yadav wrote:

How can I validate and email address without using complex reg-ex is
there a simple rails way to do this?

Are you nuts? That's just the sort of thing Regexp was designed to do.
And, it's not really all that complex.

Yes it is. The *only* correct regexps I am aware of for e-mail
addresses are on the order of a page in length. All shorter regexps
reject some valid e-mail addresses.

Thanks Marnen. I stand corrected.

However, just to clarify, are you saying that Regexp is still the best
approach? Given that what you're saying is true, which I'm sure it is,
then I could imagine that performing this validation without using
Regexp would be even more complex and require quite a bit more code.

The best approach is to download a plugin for email validation, because
someone, somewhere, has already done all the work :wink:

Robert Walker wrote:

Marnen Laibow-Koser wrote:

Robert Walker wrote:

Rajinder Yadav wrote:

How can I validate and email address without using complex reg-ex is
there a simple rails way to do this?

Are you nuts? That's just the sort of thing Regexp was designed to do.
And, it's not really all that complex.

Yes it is. The *only* correct regexps I am aware of for e-mail
addresses are on the order of a page in length. All shorter regexps
reject some valid e-mail addresses.

Thanks Marnen. I stand corrected.

However, just to clarify, are you saying that Regexp is still the best
approach? Given that what you're saying is true, which I'm sure it is,
then I could imagine that performing this validation without using
Regexp would be even more complex and require quite a bit more code.

The best approach is to download a plugin for email validation, because
someone, somewhere, has already done all the work :wink:

Several some ones...

http://agilewebdevelopment.com/plugins/validates_as_email
http://agilewebdevelopment.com/plugins/validates_as_email_address
http://agilewebdevelopment.com/plugins/validates_email_format_of

Philip Hallstrom wrote:

http://agilewebdevelopment.com/plugins/validates_email_format_of

This one does look interesting. Even has support for checking DNS MX
records. I see more clearly now what Marnen was talking about. This
plugin certainly does use Regexp, and I have to admit it is quite
complex. :slight_smile:

Philip Hallstrom wrote:

Robert Walker wrote:

Marnen Laibow-Koser wrote:

Robert Walker wrote:

Rajinder Yadav wrote:

How can I validate and email address without using complex reg-ex is
there a simple rails way to do this?

Are you nuts? That's just the sort of thing Regexp was designed to do.
And, it's not really all that complex.

Yes it is. The *only* correct regexps I am aware of for e-mail
addresses are on the order of a page in length. All shorter regexps
reject some valid e-mail addresses.

Thanks Marnen. I stand corrected.

However, just to clarify, are you saying that Regexp is still the best
approach? Given that what you're saying is true, which I'm sure it is,
then I could imagine that performing this validation without using
Regexp would be even more complex and require quite a bit more code.

The best approach is to download a plugin for email validation, because
someone, somewhere, has already done all the work :wink:

Several some ones...

http://agilewebdevelopment.com/plugins/validates_as_email
http://agilewebdevelopment.com/plugins/validates_as_email_address
http://agilewebdevelopment.com/plugins/validates_email_format_of

Thanks Philip =)

Aldric Giacomoni wrote:

Robert Walker wrote:

Marnen Laibow-Koser wrote:

Robert Walker wrote:

Rajinder Yadav wrote:

How can I validate and email address without using complex reg-ex is
there a simple rails way to do this?

Are you nuts? That's just the sort of thing Regexp was designed to do.
And, it's not really all that complex.

Yes it is. The *only* correct regexps I am aware of for e-mail
addresses are on the order of a page in length. All shorter regexps
reject some valid e-mail addresses.

Thanks Marnen. I stand corrected.

However, just to clarify, are you saying that Regexp is still the best
approach? Given that what you're saying is true, which I'm sure it is,
then I could imagine that performing this validation without using
Regexp would be even more complex and require quite a bit more code.

The best approach is to download a plugin for email validation, because
someone, somewhere, has already done all the work :wink:

I wouldn't trust those plugins without inspecting their algorithms. I
would probably use something like the following regex:
/^.+@.+\..+$/

This checks that it's of the form "something@something.something", but
performs no further validation.

Best,

Aldric Giacomoni wrote:

Robert Walker wrote:

Marnen Laibow-Koser wrote:

Robert Walker wrote:

Rajinder Yadav wrote:

How can I validate and email address without using complex reg-ex is
there a simple rails way to do this?

Are you nuts? That's just the sort of thing Regexp was designed to do.
And, it's not really all that complex.

Yes it is. The *only* correct regexps I am aware of for e-mail
addresses are on the order of a page in length. All shorter regexps
reject some valid e-mail addresses.

Thanks Marnen. I stand corrected.

However, just to clarify, are you saying that Regexp is still the best
approach? Given that what you're saying is true, which I'm sure it is,
then I could imagine that performing this validation without using
Regexp would be even more complex and require quite a bit more code.

The best approach is to download a plugin for email validation, because
someone, somewhere, has already done all the work :wink:

I wouldn't trust those plugins without inspecting their algorithms. I
would probably use something like the following regex:
/^.+@.+\..+$/

This checks that it's of the form "something@something.something", but
performs no further validation.

Good point. It annoys me to no end when I can't enter philip+sitename@pjkh.com as a valid email address. Seems this works about half the time.

The bottom line is if you are really concerned about getting a valid email address, the only way to completely verify it is to send them an email and make them confirm it via a link in that email. Headache, yeah, but it works.

validates_format_of :email, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i, :on => :create
  end