1: [MaxLength(20)]
2: [Required]
3: public string FirstName { get; set; }
4:
5: [MaxLength(20)]
6: [Required]
7: public string LastName { get; set; }
8:
9: [Required]
10: [Date(ErrorMessage = "You have to be at legal age")]
11: [DataTypeAttribute(DataType.Date)]
12: public DateTime BirthDate { get; set; }
13:
14: [Required]
15: [EnumDataType(typeof(Sex))]
16: public Sex Sex { get; set; }
17:
18: [Required]
19: [DataType(DataType.EmailAddress)]
20: [Display(Name = "Email address")]
21: public string Email { get; set; }
22:
23: [Phone]
24: public string Phone { get; set; }
You can define which properties are required, input max length, custom error messages, custom types and built in regular expressions (like email or phone).
However there not enough data annotations in the framework: you can’t define a property that will be only numeric, or credit card number and more. Yes you can define a custom regular expression but who likes to define them?
Let me introduce the “DataAnnotationsExtensions” – an open source library that gives you more attributes like “Digits” or “CraditCard” and more.
Enjoy.
No comments:
Post a Comment