Suppose the user enters 06/27/2010
in the text field and submits the form.
The default model binder runs and tries to bind what the user entered to your model. The
BirthDate
property is of typeDateTime
and as you know this type contains an hour part. So nowBirthDate = 6/27/2010 12:00:00 AM
Validation kicks in next. The
BirthDate
property is decorated with theRegularExpression
attribute so theIsValid
method is called with the string value of6/27/2010 12:00:00 AM
which fails against your pattern.The corresponding error message is shown to the user.
So the usage of this regular expression attribute is questionable. RegularExpressionAttribute works fine with string properties though.
If the user enters an invalid date the model binder will protest anyway much before the validation happens. It will also check for invalid days and months which your regular expression doesn't.