Guest User

Untitled

a guest
Oct 18th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.62 KB | None | 0 0
  1. [Required]
  2. [MinimumAge(18, ErrorMessage = "must be 18 years old or up")]
  3. [Display(Name = "Birth date")]
  4. public DateTime birthdate { get; set; }
  5.  
  6. public class MinimumAgeAttribute : ValidationAttribute
  7. {
  8. int _minimumAge;
  9.  
  10. public MinimumAgeAttribute(int minimumAge)
  11. {
  12. _minimumAge = minimumAge;
  13. }
  14.  
  15. public override bool IsValid(object value)
  16. {
  17. DateTime date;
  18. if (DateTime.TryParse(value.ToString(), out date))
  19. {
  20. return date.AddYears(_minimumAge) < DateTime.Now;
  21. }
  22.  
  23. return false;
  24. }
  25. }
  26.  
  27. if (DateTime.TryParse(value.ToString(), out date))
Add Comment
Please, Sign In to add comment