It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center




已关闭8年。




我在具有电子邮件字段的Rails应用程序中有一个User模型。是否有默认验证可确保电子邮件格式正确?如果没有,我将如何验证该字段?

最佳答案

添加您的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

关于ruby-on-rails - 如何验证ActiveRecord中电子邮件字段的格式? ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13784845/

10-15 20:35