2017-02-14 98 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