Ruby电子邮件检查(RFC 2822) [英] Ruby email check (RFC 2822)

查看:112
本文介绍了Ruby电子邮件检查(RFC 2822)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人知道Ruby中正则表达式验证电子邮件地址是否符合RFC 2822电子邮件格式?



我想要做的是: p>

  string.match(RFC_2822_REGEX)

其中RFC_2822_REGEX是用于验证我的字符串是否是有效的RFC 2882形式的正则表达式。

解决方案

p>您可以使用邮件 gem根据RFC2822解析任何字符串,如下所示:

  def valid_email(value)
begin
如果value ==
则返回false parsed = Mail :: Address.new(value)
return parsed.address == value&&& parsed.local!= parsed.address
rescue Mail :: Field :: ParseError
return false
end
end

这将检查电子邮件是否提供,即为空地址返回 false ,并检查地址是否包含一个域名。


Does anyone know what the regular expression in Ruby is to verify an email address is in proper RFC 2822 email format?

What I want to do is:

string.match(RFC_2822_REGEX)

where "RFC_2822_REGEX" is the regular expression to verify if my string is in valid RFC 2882 form.

解决方案

You can use the mail gem to parse any string according to RFC2822 like so:

def valid_email( value )
  begin
   return false if value == ''
   parsed = Mail::Address.new( value )
   return parsed.address == value && parsed.local != parsed.address
  rescue Mail::Field::ParseError
    return false
  end
end

This checks if the email is provided, i.e. returns false for an empty address and also checks that the address contains a domain.

这篇关于Ruby电子邮件检查(RFC 2822)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆