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

标签 c# jquery asp.net-mvc unobtrusive-validation email-validation

我正在使用 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>

我还从客户端验证中提取了正则表达式,以下行返回 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即使这个确切的演示 code also fails .

最佳答案

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

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

jQuery 验证插件使用 RFC 5322 Internet Message Protocol specification有效电子邮件的定义。该定义不允许在本地部分使用非拉丁字母。项目的相关摘录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 .它使用术语 SMTPUTF8。检查Email address page在维基百科上获取更多信息和支持细节。

关于c# - EmailAddressAttribute 错误地将法语电子邮件地址归类为无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42164543/

相关文章:

c# - 更新 ASP.NET One Core 中的声明值

c# - 当 ListView 被禁用时,在 ListView 中启用分页。 ASP.NET C#

c# - 使用自定义 ContractResolver 将 JSON.NET 反序列化属性名称转换为 ExpandoObject

javascript - 在十六进制颜色值的情况下使用 jquery 或 javascript 设置边框不透明度

jquery - Chris Richards 的 jQuery 半分评价

asp.net-mvc - Html.DropdownListFor 未设置选定值

asp.net-mvc - 将变量与静态 html 连接的语法

c# - 如何编译完成的 C# 项目然后在 Visual Studio 之外运行?

javascript - jQuery-UI 模态叠加

javascript - 无法使用 Tab 键选择 Kendo Multiselect 的数据