A Very simple code for date validation in vb.net.

The date formate to be given is dd/mm/yyyy.



' Validation for Number

Function NumCheck(ByVal str As String)
Dim good As String = "0123456789"
Dim i As Integer
For i = 0 To str.Length - 1 Step 1
If good.IndexOf(str.Chars(i)) = True Then
Return False
End If
Next
Return True
End Function

'Validating the Date
Function datevalidate(ByVal pres_date As String)
Dim s As String
Dim splited_date(3) As String
Dim date_pre As String
Dim month As String
Dim year As String

If (Len(Trim(pres_date))) = 0 Or pres_date.Length > 10 Then
Return False 'Display_Error(db.Fetch_Error_Description(7))
Exit Function
Else
Dim pos As Integer
pos = InStr(1, Trim(pres_date), "/")
If InStr(pos + 1, Trim(pres_date), "/") <> 0 Then
Try
s = Trim(pres_date)
splited_date = s.Split("/")
date_pre = splited_date(0)
month = splited_date(1)
year = splited_date(2)

If NumCheck(date_pre) = False Then
Return False
End If

If NumCheck(month) = False Then
Return False
End If

If NumCheck(year) = False Then
Return False
End If

Dim d As New Date(Right(s, 4), Mid(s, 4, 2), Left(s, 2))
If d > Today() Then
pres_date = ""
Return False
'Display_Error(db.Fetch_Error_Description(7))
Exit Function
End If
Catch ex As Exception
pres_date = ""
Return False
Exit Function
'Display_Error(db.Fetch_Error_Description(7))
End Try
Else
pres_date = ""
Return False
Exit Function
'Display_Error(db.Fetch_Error_Description(7))
End If
End If
Return True
End Function