[RESOLVED][DataType(DataType.EmailAddress)] Vs. [EmailAddress]

I am trying to understand what are the main differecnes between using

[DataType(DataType.EmailAddress)]

&

[EmailAddress]

inside a model class:-

public class MYViewModel {
[DataType(DataType.EmailAddress)] OR [EmailAddress]
public string Email { get; set; }

i did a test and the two attributes will do the following:-

  1. will prevent users from adding invalud email address

  2. will display the value as “EmailTo:…”

but i can not find any differences in respect to the functionality , of course if i use

html.TextboxFor

then the

Datatype

will not have any effect, while if i use

html.EditorFor

then the Datatype data annotation will work,, but i am talking about the differences in respect to the technical implementation ?

Second question will the two attributes do a server-side validation for the email format ? or it will only apply a client side validation ?

Thanks

[EmailAddress] is the MVC validation attribute. But to expand validation, MVC will convert a subset of DataType attributes to MVC validation attribute. see source code for list. As the actual MVC validation attribute is added to the field model validation
collection when a supported datatype is used, there is no difference in the how validation is done.

but as i am reading on some documentations , is that DataType should be used for formatting and not for validation, for example 
[DataType(DataType.EmailAddress)] will not do a server side validation for the email format
.,, while [EmailAddress] will do both client side & server side validation .. can u advice on this ?

Hi John,

Base on my test, the [DataType(DataType.EmailAddress)] is just apply to the client validate.

I think the different between them is that, DataType.EmailAddress will just add the attribute of
type=”Email” to the element, it won’t validate the data any more on the server. For the
EmailAddress, it will not only add that attribute to the element but also do the validation on the server.

Best Regards

Starain

so there is not any benefit for using [DataType(DataType.EmailAddress)] over using [EmailAddress] ? is this correct?

Hi join,

That’s based on the detail requirement, if we just need the client validation, we could use that to avoid server validation.

Best Regards

Starain

Leave a Reply