Подтвердите, если в нем 2 косые черты /
и day <= 31
и month <= 12
:
Dim ArrInput As Variant
ArrInput = Split(Me.DOBTextBox.Value, "/")
Dim ValidDate As Boolean
If UBound(ArrInput) = 2 Then 'make sure there are exactly two slashes in the date
If ArrInput(1) > 0 And ArrInput(1) <= 12 And _
ArrInput(0) > 0 And ArrInput(0) <= 31 Then 'month <=12 & day <= 31
ValidDate = True
End If
Else
ValidDate = False
End If
If Not ValidDate Then
MsgBox "Please enter a valid date format dd/mm/yyyy", vbCritical
DOBTextBox.SetFocus
Exit Sub
End If
'code here that executes when date is valid
Dim MyValidDate As Date
MyValidDate = DateSerial(ArrInput(2), ArrInput(1), ArrInput(0))
В качестве альтернативы просто попробуйте преобразовать строковую дату в реальную дату и проверьте,day
, month
и year
соответствуют значениям в строке.
Dim ArrInput As Variant
ArrInput = Split(Me.DOBTextBox.Value, "/")
Dim ValidDate As Boolean
If UBound(ArrInput) = 2 Then 'make sure there are exactly two slashes in the date
Dim MyValidDate As Date
MyValidDate = DateSerial(ArrInput(2), ArrInput(1), ArrInput(0))
If Day(MyValidDate) = CLng(ArrInput(0)) And _
Month(MyValidDate) = CLng(ArrInput(1)) And _
Year(MyValidDate) = CLng(ArrInput(2)) Then
ValidDate = True
End If
End If
If Not ValidDate Then
MsgBox "Please enter a valid date format dd/mm/yyyy", vbCritical
'DOBTextBox.SetFocus
Exit Sub
End If
'code here that executes when date is valid
MsgBox "date is valid " & MyValidDate