0

I have an Asp.Net MVC project. I am trying to make an email validation for an editorFor.

Based on what i've researched the way [in this answer: How to apply input which has a type email to to HTML Helper in Asp.net MVC3 Razor and more...] to do it is like this:

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

The only issue is that this makes sure that i have this pattern: name@gmail but it doesn't make sure that there is a . in the email address somewhere after the @ - [[email protected]].

any ideas about what i can do? thanks

2
  • Did you read this answer on the post you linked?
    – DavidG
    CommentedFeb 14, 2017 at 13:09
  • Remove new { @type = "email" }and add the [EmailAddress] attribute to you property
    – user3559349
    CommentedFeb 14, 2017 at 23:32

1 Answer 1

0

Use this in your model like this:

Add 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; } 

This will validate the email address properly i am using this.

1
  • I tried doing what you said @Sorangwala, but it's giving me an error - in the pattern, under the \ character there is a red line and when i hover over it it says: Unrecognized escape sequence. When i try running the program i get a "Compilation error" of: Unrecognized escape sequence.
    – Anonymous
    CommentedFeb 14, 2017 at 13:26

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.