Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#电子邮件地址验证_C#_.net_Asp.net_Regex_Validation - Fatal编程技术网

C#电子邮件地址验证

C#电子邮件地址验证,c#,.net,asp.net,regex,validation,C#,.net,Asp.net,Regex,Validation,我只想澄清一件事。根据客户机请求,我们必须创建一个正则表达式,以便在电子邮件地址中允许撇号 根据RFC标准,我的问题是电子邮件地址是否包含aportrophe?如果是,如何重新创建正则表达式以允许撇号?下面的正则表达式执行电子邮件地址的官方标准。不建议在实际应用中使用此正则表达式。这是为了说明正则表达式总是在精确性和实用性之间进行权衡 (?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\

我只想澄清一件事。根据客户机请求,我们必须创建一个正则表达式,以便在电子邮件地址中允许撇号


根据RFC标准,我的问题是电子邮件地址是否包含aportrophe?如果是,如何重新创建正则表达式以允许撇号

下面的正则表达式执行电子邮件地址的官方标准。不建议在实际应用中使用此正则表达式。这是为了说明正则表达式总是在精确性和实用性之间进行权衡

(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])
您可以使用简化的:

[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?

是的,在电子邮件中可以使用撇号,只要它不在域名中。

这是我写的验证属性。它可以验证几乎所有“原始”电子邮件地址,即本地部分@*域*形式的电子邮件地址。它不支持RFC允许的任何其他更具创造性的构造(此列表无论如何都不全面):

  • 注释(例如,
    jsmith@whizbang.com(工作)
  • 带引号的字符串(转义文本,允许atom中不允许的字符)
  • 域文字(例如
    foo@[123.45.67.012]
  • bang路径(也称为源路由)
  • 角度地址(例如,
    John Smith
  • 折叠空格
  • 本地部分或域中的双字节字符(仅限7位ASCII)
  • 等等
它应该接受几乎任何可以这样表达的电子邮件地址

  • foo。bar@bazbat.com
不需要使用引号(
)、尖括号(“”)或方括号(
[]

没有尝试验证域中最右边的dns标签是否是有效的TLD(顶级域)。这是因为TLD列表现在远大于“大6”(.com、.edu、.gov、.mil、.net、.org)加上两个字母的ISO国家/地区代码,尽管我怀疑该列表实际上并没有每天更改。此外,)还有一些电子邮件地址没有您认可的TLD(您知道
postmaster@.
理论上有效且可邮寄?发送到该地址的邮件应发送给DNS根区域的邮局主管。)

扩展正则表达式以支持域文字,应该不会太困难

给你。健康地使用它:

using System;
using System.ComponentModel.DataAnnotations;
using System.Text.RegularExpressions;

namespace ValidationHelpers
{
  [AttributeUsage( AttributeTargets.Property | AttributeTargets.Field , AllowMultiple = false )]
  sealed public class EmailAddressValidationAttribute : ValidationAttribute
  {
    static EmailAddressValidationAttribute()
    {
      RxEmailAddress = CreateEmailAddressRegex();
      return;
    }

    private static Regex CreateEmailAddressRegex()
    {
      // references: RFC 5321, RFC 5322, RFC 1035, plus errata.
      string atom             = @"([A-Z0-9!#$%&'*+\-/=?^_`{|}~]+)"                 ;
      string dot              = @"(\.)"                                            ;
      string dotAtom          =  "(" + atom + "(" + dot + atom + ")*" + ")"        ;
      string dnsLabel         = "([A-Z]([A-Z0-9-]{0,61}[A-Z0-9])?)"                ;
      string fqdn             = "(" + dnsLabel + "(" + dot + dnsLabel + ")*" + ")" ;

      string localPart        = "(?<localpart>" + dotAtom + ")"      ;
      string domain           = "(?<domain>" + fqdn + ")"            ;
      string emailAddrPattern = "^" + localPart + "@" + domain + "$" ;

      Regex instance = new Regex( emailAddrPattern , RegexOptions.Singleline | RegexOptions.IgnoreCase );
      return instance;
    }

    private static Regex RxEmailAddress;

    public override bool IsValid( object value )
    {
      string s      = Convert.ToString( value ) ;
      bool   fValid = string.IsNullOrEmpty( s ) ;

      // we'll take an empty field as valid and leave it to the [Required] attribute to enforce that it's been supplied.
      if ( !fValid )
      {
        Match m = RxEmailAddress.Match( s ) ;

        if ( m.Success )
        {
          string emailAddr              = m.Value ;
          string localPart              = m.Groups[ "localpart" ].Value ;
          string domain                 = m.Groups[ "domain"    ].Value ;
          bool   fLocalPartLengthValid  = localPart.Length >= 1 && localPart.Length <=  64 ;
          bool   fDomainLengthValid     = domain.Length    >= 1 && domain.Length    <= 255 ;
          bool   fEmailAddrLengthValid  = emailAddr.Length >= 1 && emailAddr.Length <= 256 ; // might be 254 in practice -- the RFCs are a little fuzzy here.

          fValid = fLocalPartLengthValid && fDomainLengthValid && fEmailAddrLengthValid ;

        }
      }

      return fValid ;
    }

  }
}
使用系统;
使用System.ComponentModel.DataAnnotations;
使用System.Text.RegularExpressions;
命名空间验证帮助程序
{
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field,AllowMultiple=false)]
密封的公共类EmailAddressValidationAttribute:ValidationAttribute
{
静态EmailAddressValidationAttribute()
{
RxEmailAddress=CreateEmailAddressRegex();
返回;
}
私有静态正则表达式CreateEmailAddressRegex()
{
//参考文献:RFC 5321、RFC 5322、RFC 1035及勘误表。
字符串atom=@“([A-Z0-9!#$%&'*+\-/=?^ `{124;}~]+);
字符串点=@“(\)”;
字符串dotAtom=“(“+atom+”(“+dot+atom+”)*“+”);
字符串dnsLabel=“([A-Z]([A-Z0-9-]{0,61}[A-Z0-9])?”;
字符串fqdn=“(“+dnsLabel+”(“+dot+dnsLabel+”)*“+””);
字符串localPart=“(?”+dotAtom+”);
字符串域=“(?”+fqdn+”);
字符串emailAddrPattern=“^”+localPart+“@”+domain+“$”;
Regex实例=新的Regex(emailAddrPattern,RegexOptions.Singleline | RegexOptions.IgnoreCase);
返回实例;
}
私有静态Regex RxEmailAddress;
公共覆盖布尔值有效(对象值)
{
字符串s=Convert.ToString(值);
bool fValid=string.IsNullOrEmpty;
//我们将一个空字段视为有效字段,并将其留给[Required]属性来强制执行已提供的字段。
如果(!fValid)
{
匹配m=RxEmailAddress.Match(s);
如果(m.成功)
{
字符串emailAddr=m.值;
字符串localPart=m.Groups[“localPart”].Value;
字符串domain=m.Groups[“domain”].值;

bool fLocalPartLengthValid=localPart.Length>=1&&localPart.Length=1&&domain.Length=1&&emailAddr.Length您有哪些不允许撇号的正则表达式?此链接帮助您。RFC 2822已被RFC 5322(标准草案)取代。我不知道这是否正确,但它确实有助于您显示您的工作”,并显示正则表达式是如何组成的(来自电子邮件地址的CFG?)。