dtbday.Text
will return a
String
, which could contain anything. There's no guarantee that it can be converted to a
Date
. Use
Date.TryParse[
^] to attempt to convert it to a date, and warn the user if it can't be converted.
If it can be converted, you'll want to make sure the date is not in the future. If you have other requirements for a valid birthday, then you'll need to add them as well.
Dim SelectedDate As Date
If Not Date.TryParse(dtbday.Text, SelectedDate) Then
MessageBox.Show("The entered text is not a valid date", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
ElseIf SelectedDate.Date > DateTime.Today Then
MessageBox.Show("Your date of birth cannot be in the future", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End If