# Validate email address?

**URL:** <https://www.ruby-forum.com/t/validate-email-address/127710>\
**Category:** Ruby\
**Created:** [January 31, 2008, 5:15pm UTC](https://www.ruby-forum.com/t/validate-email-address/127710 "2008-01-31T17:15:42Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![miracleofmagick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/71c47a/32.png) [@miracleofmagick](https://www.ruby-forum.com/u/miracleofmagick)\
**Post date:** [January 31, 2008, 5:15pm UTC](https://www.ruby-forum.com/t/validate-email-address/127710/1 "2008-01-31T17:15:42Z")

</div>

Anyone have a code snippet that tells whether an email address is  
“valid” but not necessarily whether it is a working address because that  
much I can sort out through my mail server through any bounces.

I have a database of addresses where the person entering the data has a  
bad, bad, bad, BAD habit of not validating them before entering them and  
so I get things like “_ **DO NOT SHARE DATA** _” and “[lmnopaol.com](http://lmnopaol.com)” and  
the like. It’s not my company or else she’d be going over all the  
entries by hand.

Thanks in advance

---

<div class="post-metadata">

**Author:** ![miracleofmagick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/71c47a/32.png) [@miracleofmagick](https://www.ruby-forum.com/u/miracleofmagick)\
**Post date:** [January 31, 2008, 6:13pm UTC](https://www.ruby-forum.com/t/validate-email-address/127710/2 "2008-01-31T18:13:22Z")

</div>

On [Fri, 01.02.2008 01:14], Eric H. wrote:

> Thanks in advance  
> email = “removed\_email\_address@domain.invalid”  
> email2 = “email@removed\_email\_address@domain.invalid”

email =~ /^[A-Z0-9._%±]+@[A-Z0-9.-]+.[A-Z]{2,4}$/i  
#=\> true/0  
email2 =~ /^[A-Z0-9._%±]+@[A-Z0-9.-]+.[A-Z]{2,4}$/i  
#=\> nil/false

I hope that helped.  
(That strange looking thing is called a regular expression)

---

<div class="post-metadata">

**Author:** ![miracleofmagick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/71c47a/32.png) [@miracleofmagick](https://www.ruby-forum.com/u/miracleofmagick)\
**Post date:** [January 31, 2008, 6:14pm UTC](https://www.ruby-forum.com/t/validate-email-address/127710/3 "2008-01-31T18:14:33Z")

</div>

On Fri, Feb 01, 2008 at 01:14:56AM +0900, Eric H. wrote:

> Anyone have a code snippet that tells whether an email address is  
> “valid” but not necessarily whether it is a working address because that  
> much I can sort out through my mail server through any bounces.
> 
> I have a database of addresses where the person entering the data has a  
> bad, bad, bad, BAD habit of not validating them before entering them and  
> so I get things like “_ **DO NOT SHARE DATA** _” and “[lmnopaol.com](http://lmnopaol.com)” and  
> the like. It’s not my company or else she’d be going over all the  
> entries by hand.

See [How to Find or Validate an Email Address](http://www.regular-expressions.info/email.html)

> Thanks in advance  
> –Greg

---

<div class="post-metadata">

**Author:** ![miracleofmagick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/71c47a/32.png) [@miracleofmagick](https://www.ruby-forum.com/u/miracleofmagick)\
**Post date:** [January 31, 2008, 8:00pm UTC](https://www.ruby-forum.com/t/validate-email-address/127710/4 "2008-01-31T20:00:35Z")

</div>

On Jan 31, 2008, at 11:55 AM, Phrogz wrote:

> Here are some more syntactically valid email addresses:  
> "Me [Here"@phrogz.net](mailto:Here%22@phrogz.net)  
> phrogz@[69.46.18.236]  
> phrogz@(go)69.46.18.236

one approach i’ve used:

address.scan( %r/[^@]+/ ).size == 2

😉

a @ [http://codeforpeople.com/](http://codeforpeople.com/)

---

<div class="post-metadata">

**Author:** ![miracleofmagick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/71c47a/32.png) [@miracleofmagick](https://www.ruby-forum.com/u/miracleofmagick)\
**Post date:** [January 31, 2008, 8:07pm UTC](https://www.ruby-forum.com/t/validate-email-address/127710/5 "2008-01-31T20:07:40Z")

</div>

module ActiveRecord  
module Validations  
module ClassMethods  
# Validates whether the value of the specified attribute is a  
valid email address  
#  
# class User \< ActiveRecord::Base  
# validates\_email\_format\_of :email, :on =\> :create  
# end  
#  
# Configuration options:  
# \* message - A custom error message (default is: "  
does not appear to be a valid e-mail address")  
# \* on Specifies when this validation is active  
(default is :save, other options :create, :update)  
# \* if - Specifies a method, proc or string to call to  
determine if the validation should  
# occur (e.g. :if =\> :allow\_validation, or :if =\> Proc.new {  
|user| user.signup\_step \> 2 }). The  
# method, proc or string should return or evaluate to a true  
or false value.  
def validates\_email\_format\_of(_attr\_names)  
configuration = { :message =\> ’ does not appear to be a valid  
e-mail address’,  
:on =\> :save,  
:with =\>  
/^(([A-Za-z0-9]+\_+)|([A-Za-z0-9]+-+)|([A-Za-z0-9]+.+)|([A-Za-z0-9]+++))_[A-Za-z0-9]+@((\w+-+)|(\w+.))\*\w{1,63}.[a-zA-Z]{2,6}$/i  
}

```
    configuration.update(attr_names.pop) if 

```

attr\_names.last.is\_a?(Hash)

```
    validates_each(attr_names, configuration) do |record, attr_name, 

```

value|  
record.errors.add(attr\_name, configuration[:message]) unless  
value.to\_s =~ configuration[:with]  
end  
end  
end  
end  
end

---

<div class="post-metadata">

**Author:** ![miracleofmagick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/71c47a/32.png) [@miracleofmagick](https://www.ruby-forum.com/u/miracleofmagick)\
**Post date:** [January 31, 2008, 8:12pm UTC](https://www.ruby-forum.com/t/validate-email-address/127710/6 "2008-01-31T20:12:26Z")

</div>

On [Fri, 01.02.2008 03:55], Phrogz wrote:

> > > the like. It’s not my company or else she’d be going over all the  
> > > #=\> nil/false
> 
> Here are some more syntactically valid email addresses:  
> "Me [Here"@phrogz.net](mailto:Here%22@phrogz.net)  
> phrogz@[69.46.18.236]  
> phrogz@(go)69.46.18.236

And as nearly no website nor many email clients (i even believe, that  
some email servers will fail on this adresses)  
can handle this kind of email addresses, it shouldn’t be a huge problem  
to use this regex.  
Of course it isn’t the best solution, but it probably covers about 99%  
of all valid email adresses.  
And i prefer covering 99% of valid adresses over allowing a lot more  
invalid adresses.

---

<div class="post-metadata">

**Author:** ![miracleofmagick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/71c47a/32.png) [@miracleofmagick](https://www.ruby-forum.com/u/miracleofmagick)\
**Post date:** [January 31, 2008, 7:55pm UTC](https://www.ruby-forum.com/t/validate-email-address/127710/7 "2008-01-31T19:55:51Z")

</div>

On Jan 31, 10:12 am, Dominik H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > entries by hand.
> 
> > Thanks in advance
> 
> email = “removed\_email\_address@domain.invalid”  
> email2 = “email@removed\_email\_address@domain.invalid”
> 
> email =~ /^[A-Z0-9._%±]+@[A-Z0-9.-]+.[A-Z]{2,4}$/i  
> #=\> true/0  
> email2 =~ /^[A-Z0-9._%±]+@[A-Z0-9.-]+.[A-Z]{2,4}$/i  
> #=\> nil/false

One of my email addresses is:  
!@phrogz.net  
This is completely valid, and not even very crazy. The proliferation  
of half-assed regular expressions like the above prevents me from  
using that address on lots of web sites.

You cannot EXACTLY validate the syntax of an email address using only  
regular expressions. You can get close, but you need a much better  
regexp than the above.

Here are some more syntactically valid email addresses:  
"Me [Here"@phrogz.net](mailto:Here%22@phrogz.net)  
phrogz@[69.46.18.236]  
phrogz@(go)69.46.18.236

---

<div class="post-metadata">

**Author:** ![miracleofmagick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/71c47a/32.png) [@miracleofmagick](https://www.ruby-forum.com/u/miracleofmagick)\
**Post date:** [January 31, 2008, 9:10pm UTC](https://www.ruby-forum.com/t/validate-email-address/127710/8 "2008-01-31T21:10:54Z")

</div>

Dominik H. wrote:

> I hope that helped.  
> (That strange looking thing is called a regular expression)

Looks fine. Go figure that I’ve been programming for decades and to this  
day I still haven’t managed to learn regex but I only ever need it once  
every 5 years or so.
