On Apr 29, 2012, at 4:12 PM, Jake K wrote:

> Hi Hoboers,
> 
> I'm trying to add a function to my user model that validates the email
> address they are signing up with ends in a certain domain. I've added
> two things to my model...
> 
> 1. A function for the actual validation:
> 
>  def validate_internal_email(email)
>    domain = email.match(/\@(.+)/)[1] # isolate the domain out of
> address
>    return true if domain.to_s == "hq.example.com"
>    return false
>  end
> 
> 2. A before_create check to run the validation: (Not sure this is the
> right approach for what I'm trying to do)
> 
>  before_create do |user|
>      if validate_internal_email(:email_address) == false
>        error-messages.add("Your email must end in hq.example.com")
>    end
>  end
> 
> Basically the idea is that only users with an
> @mysubdomain.mydomain.com can register for an account. Any tips on how
> to set this up?

You could use the built-in format validator:

validates :email_address, :format => { :with => /@hq\.example\.com\z/, :message 
=> 'must end in hq.example.com' }

See http://guides.rubyonrails.org/active_record_validations_callbacks.html for 
more details.

--Matt Jones

-- 
You received this message because you are subscribed to the Google Groups "Hobo 
Users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/hobousers?hl=en.

Reply via email to