Verifying a date is valid in a masked textbox in vb.net. Also, allow masked textbox to be empty

user3272229 picture user3272229 · Feb 4, 2014 · Viewed 21.3k times · Source

I'm working in VB.Net. I have a Windows form in which I need to have users enter valid dates inbetween 01/01/1930 and 01/01/2099. I'm using masked textbox fields for my date fields. I want my date fields to validate the dates entered to make sure they are valid, but I also want the user to be able to tab through the field without having to enter any date at all. The code below appears to validate the dates. I can enter 2/29/2010, but the date will not be accepted because it is not a leap year. The problem is the field will not allow the user to leave it blank. Can someone help me to rewrite this code so that it works the way I intended?

Private Sub frmSalvageManagerEntry_Load1(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    'Validate Date Fields
    Me.mskDOL.Mask = "00/00/0000"
    Me.mskDOL.ValidatingType = GetType(System.DateTime)
    Me.mskSetupDate.Mask = "00/00/0000"
    Me.mskSetupDate.ValidatingType = GetType(System.DateTime)

End Sub

'Declaration Public Property ValidatingType As Type

'Validate the date the user enters into the mskDOL field
Private Sub mskDOL_TypeValidationCompleted(ByVal sender As Object, ByVal e As TypeValidationEventArgs) Handles mskDOL.TypeValidationCompleted

    If mskDOL.Text = "" Then
        e.Cancel = True
        Exit Sub
    End If


    If (Not e.IsValidInput) Then
        MessageBox.Show("Please input a valid date in the format mm/dd/yyyy.", "Invalid Date Entered", MessageBoxButtons.RetryCancel)
        mskDOL.Focus()
        SendKeys.Send("{End}")

    Else
        Dim UserDate As DateTime = CDate(e.ReturnValue)
        If (UserDate < "01/01/1930" Or UserDate > "01/01/2099") Then
            MessageBox.Show("Please input a valid date in the format mm/dd/yyyy.", "Invalid Date Entered", MessageBoxButtons.RetryCancel)
            e.Cancel = True
            mskDOL.Focus()
            SendKeys.Send("{End}")
        End If
    End If


End Sub

Here is the code that ended up working for me:

_________________________________________________________________________________________________
Private Sub frmSalvageManagerEntry_Load1(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    'Validate Date Fields
    Me.mskDOL.Mask = "00/00/0000"
    Me.mskDOL.ValidatingType = GetType(System.DateTime)
    Me.mskSetupDate.Mask = "00/00/0000"
    Me.mskSetupDate.ValidatingType = GetType(System.DateTime)


End Sub

 'Declaration
Public Property ValidatingType As Type

Private Sub mskDOL_TypeValidationCompleted(ByVal sender As Object, ByVal e As TypeValidationEventArgs) Handles mskDOL.TypeValidationCompleted

    'Make sure the properties of the masked textbox field in the user interface are set to the following:
    'TextMaskFormat = IncludePromptAndLiterals
    'CutCopyMaskFormat = IncludeLiterals
    'Don't add a mask to the Mask property of the masked textbox because it is written in the form load event.
    'All the other properties can be left as default


    'If the user does not have a date to enter, allow them to tab through the field.
    If mskDOL.Text = "__/__/____" Then
        Exit Sub

    End If

    'Test to see if a valid date has been input.  If not, the user will get a message asking them to reenter a valid date.
    If (Not e.IsValidInput) Then
        MessageBox.Show("Please input a valid date in the format mm/dd/yyyy.", "Invalid Date Entered", MessageBoxButtons.RetryCancel)
        mskDOL.Focus()
        SendKeys.Send("{End}")

        'If a valid date has been entered, test to see if the user has entered a date between 01/01/1930 and 01/01/2099.
        'If the user has not entered a date within the proper timeframe, they will get a message asking them to enter a date between 01/01/1930 and 01/01/2099.
    Else
        Dim DOLDate As DateTime = CDate(e.ReturnValue)
        If (DOLDate < "01/01/1930" Or DOLDate > "01/01/2099") Then
            MessageBox.Show("Please input a valid date between 01/01/1930 to 01/01/2099 in the format mm/dd/yyyy.", "Invalid Date Entered", MessageBoxButtons.RetryCancel)
            mskDOL.Focus()
            SendKeys.Send("{End}")
            e.Cancel = True

        End If
    End If

End Sub

Answer

Neolisk picture Neolisk · Feb 4, 2014

You can achieve a similar behavior with a DateTimePicker:

enter image description here

Just set these:

  • Format = Custom
  • CustomFormat = MM/dd/yyyy

Unless you need to enter dates before 1/1/1753, I see no reason why it would not work for you. Even if it does not work exactly like you want it, extending DateTimePicker may end up being less effort. For example:

With this approach, you can type a date by parts, i.e. month, day, year. To type a date as a whole in one shot - instead of creating your own custom control, you can try this workaround: