0

I read a lot of topics which mention that the primary purpose for DataType is displaying data and not validating data. So I tried the following for testing

public partial class test
    {
        public int Id { get; set; }
        [DataType(DataType.EmailAddress)]
        public string email { get; set; }
        [DataType(DataType.Date)]
        public System.DateTime date { get; set; }
    }

And I have noted that both the date & email will have validation checking,For example I can not write invalid email address or invalid date format ? So my question is why a lot of topics mention that the primary purpose for DataType is used for formatting the properties and not for validating them. While in my test I found that specifying DataType for the property such as email & Date will create validation login also? Can anyone advice? Thanks

John John
  • 1
  • 72
  • 238
  • 501
  • 1
    Not sure which articles you have been reading but as far as the `DataType` attribute is concerned, it's definitely [used for validation](http://weblogs.asp.net/srkirkland/adding-client-validation-to-dataannotations-datatype-attribute). The clue is in the fact that it derives from [ValidationAttribute](http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.validationattribute(v=vs.110).aspx). – James Jun 30 '14 at 12:13
  • for example inside the following article http://stackoverflow.com/questions/19425916/uses-of-datatype-emailaddress-in-asp-net-mvc . it mentioned the following " Per the MSDN, DataType attributes are used primarily for formatting and not validation (which you have learned). " – John John Jun 30 '14 at 12:18
  • the problem is `DataType` is a bit of a hybrid, it won't trigger server-side validation out the box but it can trigger client-side depending on the version of MVC. [This answer](http://stackoverflow.com/questions/19425916/uses-of-datatype-emailaddress-in-asp-net-mvc#21655092) pretty much sums it up. – James Jun 30 '14 at 12:25

0 Answers0