Ruby on Rails 3,多个域的电子邮件验证?

发布于 2024-10-30 01:50:49 字数 938 浏览 1 评论 0原文

嘿伙计们,我在这里遇到了一个问题。有没有办法拥有两种可接受的电子邮件格式?

你看,我正在构建的网站需要让用户注册到某个域,并且学生和工作人员共享不同的域。例如:[电子邮件受保护][电子邮件受保护]。我希望导师和学生能够注册。到目前为止我已经尝试过了:

EMAIL_STAFF_REGEX = /\A[\w+\-.][email protected]/i
email_regex = /\A[\w+\-.][email protected]/i

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

我将非常感谢任何帮助。

hey guys, I'm running into a issue 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: [email protected], and [email protected]. I want tutors, and students to be able to sign up. I've tried this so far:

EMAIL_STAFF_REGEX = /\A[\w+\-.][email protected]/i
email_regex = /\A[\w+\-.][email protected]/i

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

I would greatly appreciate any help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

皓月长歌 2024-11-06 01:50:49

我假设您将正则表达式分开,因为您希望稍后能够使用正则表达式来判断他们是导师还是学生。

尝试

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

我建议将导师列设置为实际字段,以便您可以对其进行查询,然后使用验证来确保电子邮件匹配相同的格式。

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

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?
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文