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