EmailAddressAttribute 错误地将法语电子邮件地址归类为无效

EmailAddressAttribute incorrectly categorizing French e-mail address as invalid

我正在使用 EmailAddressAttribute 用于我的模型。

问题是当我使用

的(完全有效的)电子邮件地址时
óscar@yahoo.com

说无效

型号:

public class ForgotPasswordViewModel
{
    [Required]
    [EmailAddress]
    [Display(Name = "Email")]
    public string Email { get; set; }

    public CmsContentPagesModel PageCmsContent { get; set; }

    public CmsContentPagesModel PageCmsContentInfoIcon { get; set; }
    public CmsContentPagesModel PageCmsContentRightPanel { get; set; }
}

这是属性的问题,还是我必须以某种方式指定法语电子邮件可以?

呈现的输入框:

<div class="col-md-5">
     <input class="form-control" data-val="true" data-val-email="The Email field is not a valid e-mail address." data-val-required="The Email field is required." id="Email" name="Email" type="text" value="" />
     <span class="field-validation-valid text-danger" data-valmsg-for="Email" data-valmsg-replace="true"></span>
</div>

我还从客户端验证中提取了正则表达式,以下行 returns false

/^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/.test( 'óscar@yahoo.com' );

显然符合 this standard even though the demo for this exact code also fails.

我不知道你是如何验证电子邮件地址的,我试过像字符串一样验证你的电子邮件,我的变量 isValid 已经被验证为 true。

EmailAddressAttribute testEmailAddress = new EmailAddressAttribute();
bool isValid = testEmailAddress.IsValid("óscar@yahoo.com");

希望这些有用。

如您在正则表达式中所见:

/^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/.test( 'óscar@yahoo.com' );

ó 字符不在范围内

您可以使用以下正则表达式:

/^[a-zA-Z0-9 àâäèéêëîïóôœùûüÿçÀÂÄÈÉÊËÎÏÔŒÙÛÜŸÇ.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9 àâäèéêëîïóôœùûüÿçÀÂÄÈÉÊËÎÏÔŒÙÛÜŸÇ.!#$%&'*+\/=?^_`{|}~-](?:[a-zA-Z0-9 àâäèéêëîïóôœùûüÿçÀÂÄÈÉÊËÎÏÔŒÙÛÜŸÇ.!#$%&'*+\/=?^_`{|}~-]{0,61}[a-zA-Z0-9 àâäèéêëîïóôœùûüÿçÀÂÄÈÉÊËÎÏÔŒÙÛÜŸÇ.!#$%&'*+\/=?^_`{|}~-])?(?:\.[a-zA-Z0-9 àâäèéêëîïóôœùûüÿçÀÂÄÈÉÊËÎÏÔŒÙÛÜŸÇ.!#$%&'*+\/=?^_`{|}~-](?:[a-zA-Z0-9 àâäèéêëîïóôœùûüÿçÀÂÄÈÉÊËÎÏÔŒÙÛÜŸÇ.!#$%&'*+\/=?^_`{|}~-]{0,61}[a-zA-Z0-9 àâäèéêëîïóôœùûüÿçÀÂÄÈÉÊËÎÏÔŒÙÛÜŸÇ.!#$%&'*+\/=?^_`{|}~-])?)*$/

您还可以创建一个自定义属性作为 FrenchEmailAddress,以便使用我在自定义属性

中发布的正则表达式来验证 属性
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
public class FrenchEmailAddressAttribute : ValidationAttribute
{
    public string Pattern { get; set; }
    public RegexOptions Options { get; set; }

    public FrenchEmailAddressAttribute(string pattern, RegexOptions options = RegexOptions.None)
    {
        Pattern = pattern;
        Options = options;
    }

    public override bool IsValid(object value)
    {
        return IsValid(value as string);
    }

    public bool IsValid(string value)
    {
        return string.IsNullOrEmpty(value) ? true : new Regex(Pattern, Options).IsMatch(value);
    }
}

您需要在 jQuery 验证插件中为电子邮件验证设置自定义正则表达式:

$.validator.methods.email = function( value, element ) {
  return this.optional( element ) || /INSERT_YOUR_REGEX_HERE/.test( value );
}

jQuery 验证插件使用 RFC 5322 Internet Message Protocol specification definition of a valid email. This definition disallows use of a non latin letters in local-part. Relevant excerpt from project's README.md:

IMPORTANT NOTE ABOUT EMAIL VALIDATION. As of version 1.12.0 this plugin is using the same regular expression that the HTML5 specification suggests for browsers to use. We will follow their lead and use the same check. If you think the specification is wrong, please report the issue to them. If you have different requirements, consider using a custom method. In case you need to adjust the built-in validation regular expression patterns, please follow the documentation.

电子邮件地址国际化由另一个规范定义 - RFC 6530 Overview and Framework for Internationalized Email. It uses term SMTPUTF8. Check Email address page 在维基百科上了解更多信息和支持细节。

System.ComponentModel.DataAnnotations.EmailAddressAttribute class 使用此正则表达式。

const string pattern = @"^((([a-z]|\d|[!#$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$";

并像@ThomasRainero 所说的那样验证您的电子邮件地址

问题的上下文对于确定正确答案非常重要。在大多数情况下,最好坚持使用简单的解决方案:

^\S+@\S+\.\S+$

此正则表达式将 运行 匹配某些无效电子邮件地址的风险,但会强制执行一般结构。理想情况下,您可以通过电子邮件确认框或确认电子邮件执行第二轮验证,但即使没有额外的验证,这仍应捕获大部分无意中的无效条目。