James Stafford
James Stafford

Reputation: 1054

Validating Time

I am attempting to validate that a Valid time is submitted to a maskedtextbox which accepts 24 hour time format.

the problem is not with the numbers entered but rather incomplete times entered (13:__ etc)

    If IsDate(MaskedTextBox1.Text) Then
        MsgBox("Is a time")
    Else
        MsgBox("not a time")
    End If

seemed like the right choice until I tried another off the wall attempt entering 1_:1 is accepted as "Is a time"- so is 1:1_ - what would be a better method of validating I have a minimum time of 00:00 and a maximum time of 23:59 with no blanks.

Conclusion:

            If IsDate(MaskedTextBox1.Text) And MaskedTextBox1.MaskFull Then
                        MsgBox("Is a time")
            Else
                MsgBox("not a time")
            End If

Upvotes: 0

Views: 2094

Answers (2)

SSS
SSS

Reputation: 5393

It's very fiddly, but you can use TimeSpan.TryParseExact

Imports System.Globalization
Public Class Form1
  Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    ' Parse hour:minute value with "g" specifier current culture.'
    Dim interval As TimeSpan
    Dim format As String
    Dim culture As CultureInfo
    format = "g"
    culture = CultureInfo.CurrentCulture
    If TimeSpan.TryParseExact(TextBox1.Text, format, culture, interval) Then
      MsgBox(String.Format("'{0}' --> {1}", TextBox1.Text, interval))
    Else
      MsgBox(String.Format("Unable to parse {0}", TextBox1.Text))
    End If
  End Sub
End Class

Upvotes: 0

Mr Wednesday
Mr Wednesday

Reputation: 552

Try MaskedTextBox.MaskFull

Gets a value indicating whether all required and optional inputs have been entered into the input mask.

Upvotes: 1

Related Questions