What is the best/easy way to validate an email address in Ruby?

前端 未结 10 922
刺人心
刺人心 2020-12-13 06:40

What is the best/easy way to validate an email address in ruby (on the server side)?

10条回答
  •  无人及你
    2020-12-13 07:00

    You could look whether or not it matches a regexp like the one used in this Rails validator:

    validates_format_of :email,:with => /\A[^@\s]+@([^@\s]+\.)+[^@\s]+\z/
    

    But if you use Devise, simply do:

    validates_format_of :email,:with => Devise::email_regexp
    

    Source: http://lindsaar.net/2008/4/14/tip-4-detecting-a-valid-email-address

    Edit 1:

    useful website for tests: http://www.rubular.com/

提交回复
热议问题