Thanks jindon.
But, It's not fulfilled my requirement. I want following validations.
* Date format should be in "DD/MM/YYYY"
* "DD-MM-YYYY" should be invalid format
* "DD.MM.YYYY" should be invalid format
* "D/M/YYY" should be invalid format
* "YYYY/MM/DD" should be invalid format
* "YYY/MM/DD" should be invalid format
* "YYYY-MM-DD" should be invalid format
* "YYYY.MM.DD" should be invalid format
Should be accepted only the "DD/MM/YYYY" format as valid format. all other formats should be rejected. Also, date and month should be validated for number of days "30" and "31". In addition to that February month only for "28" and "29" days.
If you could enhance following code, it will be easy. because, already it has validated some validations. Thank you so much for your great help.
Function ValidateDate(ByVal pvDateCode) As Boolean
Dim D As Integer
Dim LeapYear As Boolean
Dim M As Integer
Dim Result As Boolean
Dim Y As Integer, i As Integer
Dim sDte As String, sPart As String
'If Not IsNumeric(pvDateCode) Then Exit Function
sDte = Format(pvDateCode, "dd/mm/yyyy")
i = InStr(pvDateCode, "/")
If i = 0 Then
MsgBox "Invalid date"
Else
D = Left(sDte, i - 1)
sPart = Mid(sDte, i + 1)
i = InStr(sPart, "/")
M = Left(sPart, i - 1)
i = InStrRev(sDte, "/")
Y = Mid(sDte, i + 1)
End If
If M > 12 Then Result = False: GoTo endit
If Y < 1900 Then Result = False: GoTo endit
LeapYear = (Y Mod 100 <> 0 And Y Mod 4 = 0) Or (Y Mod 400 = 0)
Select Case M
Case 1, 3, 5, 7, 8, 10, 12
If D >= 1 And D <= 31 Then Result = True
Case 2
If LeapYear Then
If D >= 1 And D <= 29 Then Result = True
Else
If D >= 1 And D <= 28 Then Result = True
End If
Case 4, 6, 9, 11
If D >= 1 And D <= 30 Then Result = True
End Select
'endit:
'If Not Result Then MsgBox "Format: dd/mm/yyyy"
'ValidateDate = Result
endit:
ValidateDate = Result
End Function
Bookmarks