Validate DateTime with DataAnnotation [duplicate]
I'm developing and asp.net app and i'm using data annotations to validate my Input model. In this model, I have one field of type DateTime, and I开发者_开发技巧'd like to know how could I customize the message when the user set an date value invalid.
My property in my model:
[Required(ErrorMessage = "Informe sua data de nascimento.")]
[MinAge(Idade = 18, ErrorMessage = "Você deve possuir no mínimo 18 anos para se cadastrar neste website.")]
public virtual DateTime DataNascimento { get; set; }
My model is thowing a message like this: "The value '45/64/5646' is not valid for DataNascimento."
If you could help me I appretiate!
PS: The messages of validators are in pt-br (because it'll be the language of the application)
Thanks a lot
I think what you need there is a regular expression that actually checks the format of the date time. (Note: I think this is the correct RegEx format.
[RegularExpression(@"^([0]?[1-9]|[1|2][0-9]|[3][0|1])[./-]([0]?[1-9]|[1][0-2])[./-]([0-9]{4}|[0-9]{2})$", ErrorMessage = "The date is invalid.")]
精彩评论