3

Does Rails 3 or Ruby have a built in way to return true or false on if a var is an EMAIL ADDRESS?

example

1 returns false
dadadad@asdasd.net returns true
blah+asdasds@asdasd.net returns true
asdasdasdads returns false

Maybe something like, if 1.is_a? Email end

thanks

JW8
  • 1,496
  • 5
  • 21
  • 36
AnApprentice
  • 108,152
  • 195
  • 629
  • 1,012
  • The problem with trying to validate an email address is that the allowed address formats are too varied to handle with a simple regex. Even a complex regex can't catch all the corner cases, but, even if one could determine if the address format was correct, it still wouldn't be able to tell if it was a real address. Because of the complexity of the problem there are two solutions: send an email to the address and see if the receiver responds, or do a simplistic check for user id, an '@' and a domain, and call it good enough. The second is trivially easy to spoof. – the Tin Man Jan 04 '11 at 04:54
  • For an in-depth discussion of this topic, see [How to use a regular expression to validate an email addresses?](http://stackoverflow.com/questions/201323/how-to-use-a-regular-expression-to-validate-an-email-addresses) – Seth Bro Mar 22 '12 at 21:39

4 Answers4

7

I ended up using the Rails gem, EmailVeracity which worked great

Nuriel
  • 3,731
  • 3
  • 23
  • 23
AnApprentice
  • 108,152
  • 195
  • 629
  • 1,012
5

Something like this should work:

def isEmail(str)
  return str.match(/[a-zA-Z0-9._%]@(?:[a-zA-Z0-9]\.)[a-zA-Z]{2,4}/)
end
Andrew Grimm
  • 78,473
  • 57
  • 200
  • 338
Austin M
  • 594
  • 1
  • 4
  • 19
  • 1
    You can't validate email addresses using a regular expression. http://stackoverflow.com/questions/201323/what-is-the-best-regular-expression-for-validating-email-addresses/201378#201378 – Andrew Grimm Jan 04 '11 at 02:06
  • 2
    @Andrew then what should I use if I can use a regex? – AnApprentice Jan 04 '11 at 02:12
  • 4
    This wouldn't work for most domains, because you allow domain name to be just single letter, like foo@a.com. Probably you wanted to write this one: `/[a-zA-Z0-9._%]@(?:[a-zA-Z0-9]+\.)[a-zA-Z]{2,4}/` – Tombart Dec 19 '14 at 12:09
1

In a model you can use validators (in ActiveModel) along with a

something like this:

class User
  include ActiveModel::Validations

  validates :email, :presence => true,
                    :format => {:with => /^[^@\s]+)@((?:[-a-z0-9A-Z]+\.[a-zA-Z]{2,})$/}

end

see http://yehudakatz.com/2010/01/10/activemodel-make-any-ruby-object-feel-like-activerecord/ for more on ActiveModel

Joshua Smith
  • 6,561
  • 1
  • 30
  • 28
  • You can't validate email addresses using a regular expression. http://stackoverflow.com/questions/201323/what-is-the-best-regular-expression-for-validating-email-addresses/201378#201378 – Andrew Grimm Jan 04 '11 at 02:05
  • He only asked for a subset of email addresses, not the whole RFC. – Joshua Smith Jan 04 '11 at 02:44
  • By that logic, `def is_email(str) str =~ /asd/ end` would do what he literally asked for? – Andrew Grimm Jan 04 '11 at 03:30
  • There's a syntax error in that regular expression `^([^@\s]+)@([-a-z0-9A-Z]+)\.([a-zA-Z]{2,})$` would match at least some email address. – Tombart Dec 19 '14 at 12:13
0

Use https://github.com/OpenGems/rails_email_checker

ActiveModel email validation. Checks MX records, sub address, regex, whitelisted and blacklisted check

Boris BRESCIANI
  • 527
  • 7
  • 16