有没有办法验证电子邮件地址,例如'myemail@mydomain.com'
?
所以我应该检查用户是否在注册时有@mydomain.com
分机?正则表达式代码会是什么样的?
这是我目前正在使用的内容,如果验证通过user.type = special
,则需要添加@mydomain.com
if user.type = normal
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i
validates :email, presence: true, length: { maximum: 255 },
format: { with: VALID_EMAIL_REGEX },
uniqueness: { case_sensitive: false }
答案 0 :(得分:1)
您可以在电子邮件属性
上添加模型级别验证validates :email, format: { with: /@mydomain\.com\z/i }
但是,上述正则表达式对@
编辑:
您必须添加一个检查@ mydomain.com
的before_create操作before_create :set_user_type
def set_user_type
if /@mydomain\.com\z/i.match(email)
self.type = :special
else
self.type = :normal
end
end