Simple Boolean conditions can be confusing sometimes (I also submitted a wrong answer at first but immediately deleted it). Your condition was correct for determining if the date was OK:
if (dtpDOB.Value < DateTime.Today.AddYears(-18)
&& dtpDOB.Value > DateTime.Today.AddYears(-100))
{
// the date entered was valid!
}
You can put an else block after that and handle the unwanted input there.
Note that you check for the "left" endpoint of the interval last. Today minus 100 years is the earlier of the two points in time.
I think you wanted to express the negation. That is like this:
if (!(dtpDOB.Value < DateTime.Today.AddYears(-18)
&& dtpDOB.Value > DateTime.Today.AddYears(-100)))
{
// the date entered was bad, outside permitted range
}
or equivalently, by de Morgan's law:
if (dtpDOB.Value >= DateTime.Today.AddYears(-18)
|| dtpDOB.Value <= DateTime.Today.AddYears(-100))
{
// the date entered was bad, outside permitted range
}
Here I used the "algebraic" fact that
!(x < b && x > a)
is logigally equivalent to
x >= b || x <= a
where the "strict" < reverses and becomes non-strict upon negation.
You should be sure what behavior you want if today is actually the 18th or 100th birthday of the user.
dateTimePicker1.MinDate = DateTime.Today.AddYears(-18);so the min date is always today least 18