Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3789442
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T12:09:27+00:00 2026-05-19T12:09:27+00:00

for my rails3, devise, users model (name, email, etc…) I want to prevent bad

  • 0

for my rails3, devise, users model (name, email, etc…) I want to prevent bad domains from registering on the site.

The idea being I have a list of blacklisted domains (badplace.com, hotmail.com) … and when a new user record goes to be saved, I check the email, if it has a domain with a bad domain, I add an error.

So what’s the right way to implement this smartly in Rails…

Here’s what I’ve been playing with:

In the User’s Model

protected
  validates_each :email, :on => :create do |record, attr, value|
     domain = email.split("@").last
     record.errors.add attr, "That's a BAD EMAIL." unless value && !value.contains(domain)
  end

What do you think?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T12:09:27+00:00Added an answer on May 19, 2026 at 12:09 pm

    You can do this more easily with a validates_format_of and a regular expression:

    class User < ActiveRecord::Base
      validates_format_of :email, :without => /badplace\.com|hotmail\.com/, :message => "That's a BAD EMAIL."
    end
    

    EDIT:

    For many addresses, you can do something like this:

    INVALID_EMAILS = %w(badplace.com hotmail.com)
    validates_format_of :email, :without => /#{INVALID_EMAILS.map{|a| Regexp.quote(a)}.join('|')}/, :message => "That's a BAD EMAIL."
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Application-Stack: Rails3, CanCan, Devise, Shoulda I've got some nested Resources and want to test
I'm using rails 2.3.5 and devise 1.0.6. I'm having users confirm account's with email.
I've recently just added Devise to my first Rails3 app, and I'm having a
I have use devise_openid_authenticatable to support OpenID with devise in rails3 beta4. But when
Using the current rails 2 I want users to be able to create an
rails 3 newbie, using Devise for auth... I want to create the following models:
Help, I've broken my Rails3 user model! I first noticed that I could not
Rails 2.3.9 Devise 1.0.8 I'm trying to restrict registrations for a Devise User model
i moved from php to rails3, and i still think it was a good
Rails uses the concept of migrations to deal with model changes using the ActiveRecord

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.