开发者

Ruby on Rails 3, Email Validation for Multiple Domains?

hey guys, I'm running into a i开发者_运维问答ssue here. Is there a way to have TWO acceptable email formats?

You see, the site I'm building needs to have users register with a certain domain, and the student, and staff share different domains. For Example: name@mail.domain.ac.uk, and name@domain.ac.uk. I want tutors, and students to be able to sign up. I've tried this so far:

EMAIL_STAFF_REGEX = /\A[\w+\-.]+@mail.domain.ac.uk/i
email_regex = /\A[\w+\-.]+@domain.ac.uk/i

validates :email, :presence   => true,
                  :format     => { :with => email_regex,
                                   :with => EMAIL_STAFF_REGEX},}

I would greatly appreciate any help.


I'm assuming you are splitting the regexp's out because you want to be able to use the regexp later to tell if they are a tutor or student.

Try

tutor_subdomain = mail
EMAIL_REGEX = /\A[\w+-.]+@(:?#{tutor_domain}.)?domain.ac.uk/i
TUTOR_REGEX = /\A[\w+-.]+@#{tutor_domain}.domain.ac.uk/i

def tutor?
  email =~ TUTOR_REGEX
end

I would recommend making the tutor column an actual field so you can do queries on it and then use validations to make sure that the email matches the same format.

validates:email, :format => {:with => STUDENT_REGEX}
validates:email, :format => {:with => TUTOR_REGEX}, :if => :tutor?
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜