# Easy Validation Question

**URL:** <https://www.ruby-forum.com/t/easy-validation-question/121980>\
**Category:** Rails\
**Created:** [December 3, 2007, 4:43pm UTC](https://www.ruby-forum.com/t/easy-validation-question/121980 "2007-12-03T16:43:31Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![joep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/87869e/32.png) [@joep](https://www.ruby-forum.com/u/joep)\
**Post date:** [December 3, 2007, 4:43pm UTC](https://www.ruby-forum.com/t/easy-validation-question/121980/1 "2007-12-03T16:43:31Z")

</div>

Hey,

I’m trying to update the validation of one of my models to not allow  
emails from “[bad-domain.com](http://bad-domain.com)”

I already have this in my model:

validates\_format\_of :email,  
:with =\>  
%r{.(edu|com|gov|tv|biz|net|org|info|us|name|uk|jobs|tk|nz|de)$}i,  
:message =\> “Must be a valid email address”

That is working. But what kind of validation can I use to make sure  
that the :email doesn’t contain “[bad-domain.com](http://bad-domain.com)”?

---

<div class="post-metadata">

**Author:** ![joep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/87869e/32.png) [@joep](https://www.ruby-forum.com/u/joep)\
**Post date:** [December 3, 2007, 5:47pm UTC](https://www.ruby-forum.com/t/easy-validation-question/121980/2 "2007-12-03T17:47:16Z")

</div>

Joe P. wrote:

> Hey,
> 
> I’m trying to update the validation of one of my models to not allow  
> emails from “[bad-domain.com](http://bad-domain.com)”
> 
> I already have this in my model:
> 
> validates\_format\_of :email,  
> :with =\>  
> %r{.(edu|com|gov|tv|biz|net|org|info|us|name|uk|jobs|tk|nz|de)$}i,  
> :message =\> “Must be a valid email address”
> 
> That is working. But what kind of validation can I use to make sure  
> that the :email doesn’t contain “[bad-domain.com](http://bad-domain.com)”?

Hmm, I was wondering if validates\_exclusion\_of would work, or is that  
for something else?

---

<div class="post-metadata">

**Author:** ![joep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/87869e/32.png) [@joep](https://www.ruby-forum.com/u/joep)\
**Post date:** [December 4, 2007, 5:50pm UTC](https://www.ruby-forum.com/t/easy-validation-question/121980/3 "2007-12-04T17:50:05Z")

</div>

It seems like this would be easy, I just can’t find info on how to do  
this…

---

<div class="post-metadata">

**Author:** ![joep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/87869e/32.png) [@joep](https://www.ruby-forum.com/u/joep)\
**Post date:** [December 4, 2007, 8:35pm UTC](https://www.ruby-forum.com/t/easy-validation-question/121980/4 "2007-12-04T20:35:02Z")

</div>

On Dec 3, 2007, at 7:43 AM, Joe P. wrote:

> That is working. But what kind of validation can I use to make sure  
> that the :email doesn’t contain “[bad-domain.com](http://bad-domain.com)”?

Unless I’m missing what you’re trying to do, it would be the exact  
same approach:

validates\_format\_of :email,  
:with =\> /bad-domain.com$/i  
:message =\> “[bad-domain.com](http://bad-domain.com) not allowed”

You might want to add this too (verifies it’s at least removed\_email\_address@domain.invalid) and  
then your original one can test the TLD specifically.

validates\_format\_of :email,  
:with =\> /^[A-Z0-9._%-]+@[A-Z0-9._%-]+.[A-Z]{2,4}$/i  
:message =\> “Must be a valid email address”

> Hmm, I was wondering if validates\_exclusion\_of would work, or is that  
> for something else?

This is primarily for testing value lists. It verifies that the  
submitted value is within a specified set of options.

–  
def gw  
acts\_as\_n00b  
writes\_at(www.railsdev.ws)  
end

---

<div class="post-metadata">

**Author:** ![joep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/87869e/32.png) [@joep](https://www.ruby-forum.com/u/joep)\
**Post date:** [December 5, 2007, 6:24pm UTC](https://www.ruby-forum.com/t/easy-validation-question/121980/5 "2007-12-05T18:24:31Z")

</div>

> Unless I’m missing what you’re trying to do, it would be the exact  
> same approach:
> 
> validates\_format\_of :email,  
> :with =\> /bad-domain.com$/i  
> :message =\> “[bad-domain.com](http://bad-domain.com) not allowed”

Won’t this just validate that the email matches this format? I’m trying  
to get it so it accepts valid email addresses, but not if they’re from  
“[baddomain.com](http://baddomain.com)”

---

<div class="post-metadata">

**Author:** ![joep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/87869e/32.png) [@joep](https://www.ruby-forum.com/u/joep)\
**Post date:** [December 5, 2007, 7:37pm UTC](https://www.ruby-forum.com/t/easy-validation-question/121980/6 "2007-12-05T19:37:47Z")

</div>

Greg W. wrote:

> On Dec 5, 2007, at 9:24 AM, Joe P. wrote:
> 
> > “[baddomain.com](http://baddomain.com)”  
> > Duh. Brain fade. Use validates\_each.
> 
> validates\_each :email do |model, attr, value|  
> if value =~ /baddomain.com/i  
> model.errors.add(attr, “[bad-domain.com](http://bad-domain.com) not allowed”)  
> end  
> end
> 
> –  
> def gw  
> acts\_as\_n00b  
> writes\_at(www.railsdev.ws)  
> end

Thanks, it works like a charm. I didn’t even think of using  
“validates\_each”

---

<div class="post-metadata">

**Author:** ![joep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/87869e/32.png) [@joep](https://www.ruby-forum.com/u/joep)\
**Post date:** [December 5, 2007, 7:06pm UTC](https://www.ruby-forum.com/t/easy-validation-question/121980/7 "2007-12-05T19:06:50Z")

</div>

On Dec 5, 2007, at 9:24 AM, Joe P. wrote:

> “[baddomain.com](http://baddomain.com)”  
> Duh. Brain fade. Use validates\_each.

validates\_each :email do |model, attr, value|  
if value =~ /baddomain.com/i  
model.errors.add(attr, “[bad-domain.com](http://bad-domain.com) not allowed”)  
end  
end

–  
def gw  
acts\_as\_n00b  
writes\_at(www.railsdev.ws)  
end
