Easy Validation Question

Hey,

I’m trying to update the validation of one of my models to not allow
emails from “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”?

Joe P. wrote:

Hey,

I’m trying to update the validation of one of my models to not allow
emails from “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”?

Hmm, I was wondering if validates_exclusion_of would work, or is that
for something else?

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

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”?

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 not allowed”

You might want to add this too (verifies it’s at least [email protected]) and
then your original one can test the TLD specifically.

validates_format_of :email,
:with => /^[A-Z0-9.%-][email protected][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

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 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

Greg W. wrote:

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

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 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”

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

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 not allowed”)
end
end


def gw
acts_as_n00b
writes_at(www.railsdev.ws)
end

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs