2017-02-14 35 views
0

我有一个Asp.Net MVC项目。我正在尝试为editorFor进行电子邮件验证。html helper的电子邮件验证 - ASP.Net

基于我所研究的方法[在这个答案:How to apply input which has a type email to to HTML Helper in Asp.net MVC3 Razor更多...]做到这一点是这样的:

@Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "form-control", @dir="ltr", @type = "email"} }) 

唯一的问题是,这可以确保我有此模式: name @ gmail 但它不确定是否有@ - [[email protected]]之后某处的电子邮件地址。

关于我能做什么的任何想法? 感谢

+0

你读过[this](http://stackoverflow.com/a/16690164/1663001)回复你关联的帖子吗? – DavidG

+0

删除'new {@type =“email”}''并将'[EmailAddress]'属性添加到你的属性 –

回答

0

使用此模型中是这样的:

添加using System.ComponentModel.DataAnnotations;

[Display(Name = "Email Address")] 
     [EmailAddress] 
     [RegularExpression("^[_A-Za-z'`+-.]+([_A-Za-z0-9'+-.]+)*@([A-Za-z0-9-])+(\\.[A-Za-z0-9]+)*(\\.([A-Za-z]*){3,})$",ErrorMessage="Enter proper email")] 
     [Required] 
     [DataType(DataType.EmailAddress, ErrorMessage = "Format not proper of email address")] 
     public string Email { get; set; } 

这将验证的电子邮件地址正确,我使用这一点。

+0

我试着做你说的@Sorangwala,但它给了我一个错误 - 在模式下,\字符有一条红线,当我将鼠标悬停在它上面时,它说:**无法识别的转义序列**。当我尝试运行程序时,出现以下错误:“无法识别的转义序列**”。 – Anonymous