9

I have a Account Model in which I am using Email Address as Username

public class RegisterModel
    {
        [Required]
        [Display(Name = "Email Address")]
        [DataType(DataType.EmailAddress)]
        public string UserName { get; set; }

I have designed a custom class to verify email. But I recently noticed that the DataType.EmailAddress. I tried to use this Datatype as shown in code above to check if I can validate the Username without my Custom Class but it fails. So my question is how is this DataType useful in .NET. It seems to be doing nothing on my Registration Form.

Edit: It dosent even validate against a regex. For example Username: SS, ssssss, tttt, etc all pass off as valid emails.

Edit: People I have a class to validate email in the code behind. I know hat are the complexities of validating Emails. I am not asking how to validate email. I am just asking about the uses of this datatype.

Flood Gravemind
  • 3,773
  • 12
  • 47
  • 79
  • 1
    You can make an EditorTemplate. – SLaks Oct 17 '13 at 11:50
  • possible duplicate of [mvc \[DataType(DataType.EmailAddress) no validation](http://stackoverflow.com/questions/11453574/mvc-datatypedatatype-emailaddress-no-validation) – ediblecode Oct 17 '13 at 11:59
  • @jumpingcode Please see my edit why this question is different. – Flood Gravemind Oct 17 '13 at 12:04
  • 1
    DataType attribute is used for formatting purposes, not for validation. – ediblecode Oct 17 '13 at 12:07
  • http://stackoverflow.com/questions/6550487/does-the-datatypeattribute-on-a-model-do-validation-in-mvc-3 – ediblecode Oct 17 '13 at 12:09
  • @jumpingcode can you please explain how that question explains what this datatype does? – Flood Gravemind Oct 17 '13 at 12:18
  • If you looked at the answers, it would tell you. Instead you just looked at the question, you have to do a bit of the work yourself you know – ediblecode Oct 17 '13 at 13:38
  • @jumpingcode - I did go look at the link and the top answer a) does not address using *DataType[EmailAddress]* and b) is using editors which do not impact what this DataType does (hint: it only changes the display template). The only answer that had remotely anything to do with the *EmailAddress* type was deleted and could not be seen by him (or you even?) – Tommy Oct 17 '13 at 15:26
  • `DataType attributes are used by the templated views when using editorfor()` – ediblecode Oct 17 '13 at 15:29
  • 1
    @jumpingcode - "impact what **this** DataType does". Not trying to start a comment war here, rather politely stating you were a little rough in your presentation as it related to the question being asked. – Tommy Oct 17 '13 at 19:53
  • @Tommy Point taken and +1 for the answer – ediblecode Oct 18 '13 at 08:19

4 Answers4

17

So, you are asking what this data type does not why isn't it validating, correct? Per the MSDN, DataType attributes are used primarily for formatting and not validation (which you have learned). What this attribute should do, is when using the Html.DisplayFor() helper, render the field as a clickable hyperlink.

@Html.DisplayFor(x=>x.UserName)

Renders

<a href="mailto:{0}">{0}</a>

Additionally, as pointed out by Zhaph in the comments below, using it in the Html.EditorFor() will generate an HTML 5 email input, which looks something like this:

<input type="email".../>

From MSDN

The following example uses the DataTypeAttribute to customize the display of EmailAddress data field of the customer table in the AdventureWorksLT database. The e-mail addresses are shown as hyperlinks instead of the simple text that ASP.NET Dynamic Data would have inferred from the intrinsic data type.

Tommy
  • 39,592
  • 10
  • 90
  • 121
  • 2
    It can also be used by the `EditorFor` helper to generate an `` HTML5 input field. – Zhaph - Ben Duguid Sep 01 '14 at 16:35
  • 1
    @Zhaph-BenDuguid - cool, that might be something new in 4.5 (which I am not sure was available back when this question was first posted :) ) - and definitely useful! – Tommy Sep 02 '14 at 03:04
13

DataType alone will not trigger any server-side validation. But, since MVC 4 using DataType.EmailAddress will make the HTML input use type="email", which in turn makes jQuery Validation perform Regex validation on the client.

.NET 4.5 introduced the [EmailAddress] attribute, a subclass of DataTypeAttribute. By using [EmailAddress] you get both client and server side validation.

Max Toro
  • 28,282
  • 11
  • 76
  • 114
  • warning: server side validation is quite simple, it checks only if "@" is part of the input string – croban Dec 21 '20 at 16:20
  • from 4.7.2 regex email validation is disabled and you have to enable it look this issue https://social.msdn.microsoft.com/Forums/en-US/0ce2ad75-33f9-4e0c-8766-9422bfb5bdc6/emailaddressattribute-not-working-correctly – croban Dec 21 '20 at 16:41
4

you can use the EmailAddress data annotation or the regex to solve this issue. Date type is used to tell the html helper to render the html for the view.

[EmailAddress]
[RegularExpression(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*", ErrorMessage = "Must be a valid Email Address")]
Anto Subash
  • 3,140
  • 2
  • 22
  • 30
2

Datatype.Emailaddress derives from DataTypeAttribute and adds client-side e-mail validation you also need to set <% Html.EnableClientValidation(); %> in your corresponding view.

Alternatively you could use the DataAnnotations library by using EmailAddress (This performs server side validation)

using System.ComponentModel.DataAnnotations; 

    [Required]
    [EmailAddress]
    public String Email { get; set; }

This is the regex to validate Email address

[Required(ErrorMessage="Email is required")]
[RegularExpression(@"[A-Za-z0-9._%+-]+[A-Za-z0-9.-]+\.[A-Za-z] {2,4}",
public String Email {get; set;}

You can also create custom email validation Attribute.

http://weblogs.asp.net/scottgu/archive/2010/01/15/asp-net-mvc-2-model-validation.aspx

Rohit
  • 10,056
  • 7
  • 50
  • 82