Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我在具有电子邮件字段的 Rails 应用程序中有一个用户模型。是否存在可确保电子邮件格式正确的默认验证?如果没有,我将如何验证该字段?
添加你的gemfile:
gem 'validates_email_format_of'
在你的模型中:
validates :email, email_format: { message: "doesn't look like an email address" }
或者,如果您不想使用 gem,请使用正则表达式:
validates_format_of :email, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i