VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



Validates the date in mm/dd/yyyy format

by Abhinav Lal (1 Submission)
Category: Miscellaneous
Compatability: VB 6.0
Difficulty: Unknown Difficulty
Originally Published: Sat 16th April 2005
Date Added: Mon 8th February 2021
Rating: (1 Votes)

Validates the date in mm/dd/yyyy format

Rate Validates the date in mm/dd/yyyy format



    Dim flag As Integer
    Dim dt As Variant
    Dim isLeapYear As Boolean
    
    ' Date in mm/dd/yyyy format
    dt = Split(mskDate, "/", -1, vbTextCompare)
    If Val(dt(2)) <= 0 Or Val(dt(2)) > Year(Date) Then
        flag = 3 ' Year out of valid range
    Else
        If Val(dt(0)) = 1 Or Val(dt(0)) = 3 Or Val(dt(0)) = 5 Or Val(dt(0)) = 7 Or Val(dt(0)) = 8 Or Val(dt(0)) = 10 Or Val(dt(0)) = 12 Then
        ' 31 day validation
            If Val(dt(1)) <= 0 Or Val(dt(1)) > 31 Then
                flag = 2 ' Date out of valid range
            End If
        ElseIf Val(dt(0)) = 4 Or Val(dt(0)) = 6 Or Val(dt(0)) = 9 Or Val(dt(0)) = 11 Or Val(dt(0)) = 8 Or Val(dt(0)) = 11 Then
        ' 30 day validation
            If Val(dt(1)) <= 0 Or Val(dt(1)) > 30 Then
                flag = 2 ' Date out of valid range
            End If
        ElseIf Val(dt(0)) = 2 Then
        ' February Error
            ' Checking for leap year
            If (Val(dt(2)) Mod 4 <> 0) Then
                isLeapYear = False
            ElseIf (Val(dt(2)) Mod 400 = 0) Then
                isLeapYear = True
            ElseIf (Val(dt(2)) Mod 100 = 0) Then
                isLeapYear = False
            End If
            
            If isLeapYear = True Then
                If Val(dt(1)) <= 0 Or Val(dt(1)) > 29 Then
                        flag = 2 ' Date out of valid range
                End If
            Else
                If Val(dt(1)) <= 0 Or Val(dt(1)) > 28 Then
                    flag = 2 ' Date out of valid range
                End If
            End If
        ElseIf Val(dt(0)) <= 0 Or Val(dt(0)) > 12 Then
            flag = 1 ' Month out of valid range
        End If
    End If
    ValidateDate = flag
End Function

Download this snippet    Add to My Saved Code

Validates the date in mm/dd/yyyy format Comments

No comments have been posted about Validates the date in mm/dd/yyyy format. Why not be the first to post a comment about Validates the date in mm/dd/yyyy format.

Post your comment

Subject:
Message:
0/1000 characters