TextBox Validation for time only, not date

I

Ian Andrews

Have used to following to validate a textbox, but this accepts dates as being
ok. I only want Time values

Private Sub TxtJanlight1on1_Validating(ByVal sender As Object, ByVal e As
System.ComponentModel.CancelEventArgs) Handles TxtJanlight1on1.Validating

If TxtJanlight1on1.Text <> "" AndAlso Not
IsDate(TxtJanlight1on1.Text) Then
MsgBox("Please enter a Valid Time Format, MUST BE AS 07:30:00,
Value has been defaulted to 00:00:00")
e.Cancel = True
TxtJanlight1on1.Text = "00:00:00"
End If

End Sub

What can I do to reject dates and only accept valid time values ?
 
A

Armin Zingler

Ian said:
Have used to following to validate a textbox, but this accepts dates
as being ok. I only want Time values

Private Sub TxtJanlight1on1_Validating(ByVal sender As Object, ByVal
e As System.ComponentModel.CancelEventArgs) Handles
TxtJanlight1on1.Validating

If TxtJanlight1on1.Text <> "" AndAlso Not
IsDate(TxtJanlight1on1.Text) Then
MsgBox("Please enter a Valid Time Format, MUST BE AS
07:30:00, Value has been defaulted to 00:00:00")
e.Cancel = True
TxtJanlight1on1.Text = "00:00:00"
End If

End Sub

What can I do to reject dates and only accept valid time values ?

A DateTime value is any point in time. None of them has no date. The default
conversion from String to DateTime assumes the 1/1/0001 if no date part is
entered. The String "15:47" is converted to the DateTime value 1/1/0001
15:47. You can compare the date part of the DateTime value to 1/1/0001 to
find out if a date has been entered. If yes, the input was invalid, too.

To store a time only, the TimeSpan type should (can) be used. It is equal to
the type of a DateTime's TimeOfDay property which only returns the time
part. You can use the TimeSpan.Parse or .TryParse methods to convert from a
String, but note that it expects a fixed format (see [F1]) and disregards
the current culture settings.

Another way is using DateTime.ParseExact where you can pass the accepted
format(s). However, the date part will still be 1/1/0001. Therefore I'd
still store the time in a TimeSpan object. See also:
http://msdn.microsoft.com/en-us/library/2h3syy57.aspx


Armin
 
I

Ian Andrews

Thank you for your help here - what your saying does make a great deal of
sense. My only problem is what would this look like in place of my code.
Being new to VB2008, have only been able to work with code samples so far -
struggle to write my own with understanding

Many Thanks

Armin Zingler said:
Ian said:
Have used to following to validate a textbox, but this accepts dates
as being ok. I only want Time values

Private Sub TxtJanlight1on1_Validating(ByVal sender As Object, ByVal
e As System.ComponentModel.CancelEventArgs) Handles
TxtJanlight1on1.Validating

If TxtJanlight1on1.Text <> "" AndAlso Not
IsDate(TxtJanlight1on1.Text) Then
MsgBox("Please enter a Valid Time Format, MUST BE AS
07:30:00, Value has been defaulted to 00:00:00")
e.Cancel = True
TxtJanlight1on1.Text = "00:00:00"
End If

End Sub

What can I do to reject dates and only accept valid time values ?

A DateTime value is any point in time. None of them has no date. The default
conversion from String to DateTime assumes the 1/1/0001 if no date part is
entered. The String "15:47" is converted to the DateTime value 1/1/0001
15:47. You can compare the date part of the DateTime value to 1/1/0001 to
find out if a date has been entered. If yes, the input was invalid, too.

To store a time only, the TimeSpan type should (can) be used. It is equal to
the type of a DateTime's TimeOfDay property which only returns the time
part. You can use the TimeSpan.Parse or .TryParse methods to convert from a
String, but note that it expects a fixed format (see [F1]) and disregards
the current culture settings.

Another way is using DateTime.ParseExact where you can pass the accepted
format(s). However, the date part will still be 1/1/0001. Therefore I'd
still store the time in a TimeSpan object. See also:
http://msdn.microsoft.com/en-us/library/2h3syy57.aspx


Armin
 
A

Armin Zingler

Ian said:
Thank you for your help here - what your saying does make a great
deal of sense. My only problem is what would this look like in place
of my code. Being new to VB2008, have only been able to work with
code samples so far - struggle to write my own with understanding

Your code is fine. You just have to do an additional check. Which of the 3
options I mentioned do you want to choose?

Example:

Dim d As DateTime

If Not DateTime.TryParseExact( _
"17:45:34", "HH:mm:ss", Nothing, _
Globalization.DateTimeStyles.None, d) Then

MsgBox("invalid")
End If

Date/time formats:
http://msdn.microsoft.com/en-us/library/97x6twsz.aspx


Armin
 
A

Armin Zingler

Ian said:
Thanks again,
Have tried your example, but unfortunately, any text in date format
is still accepted.
All I wish to do is have any valid times accepted, which must not
include a date value.

If 12/12/09 entered, reject entry
if 17:45:00 entered, accept entry
any other entry types to be rejected. even 12/12/09 17:45:00 must be
rejected.

any of your 3 options would be ok, if they can contain the above.

I tried my example and it does exactly what you want.
"12/12/09" => invalid
"17:45:00" => valid
"12/12/09 17:45:00" => invalid

Please post your current code.


Armin
 
I

Ian Andrews

Current Code for this textbox validation is as follows,

Private Sub TxtJanlight1on1_Validating(ByVal sender As Object, ByVal e As
System.ComponentModel.CancelEventArgs) Handles TxtJanlight1on1.Validating

Dim d As DateTime

If Not DateTime.TryParseExact("17:00:00", "HH:mm:ss", Nothing,
Globalization.DateTimeStyles.None, d) Then
MsgBox("invalid")
e.Cancel = True
TxtJanlight1on1.Text = "00:00:00"
End If


If TxtJanlight1on1.Text <> "" AndAlso Not
IsDate(TxtJanlight1on1.Text) Then
MsgBox("Please enter a Valid Time Format, MUST BE AS 07:30:00,
Value has been defaulted to 00:00:00")
e.Cancel = True
TxtJanlight1on1.Text = "00:00:00"
End If

'Testtext.Text = d

End Sub
 
A

Armin Zingler

Ian said:
If Not DateTime.TryParseExact("17:00:00", "HH:mm:ss", Nothing,
Globalization.DateTimeStyles.None, d) Then

If Not DateTime.TryParseExact(TxtJanlight1on1.text, "HH:mm:ss",
Nothing, Globalization.DateTimeStyles.None, d) Then



Armin
 
C

Cor Ligthert[MVP]

Ian,

Is it not easier to use a datetimepicker instead of a text box with the
property format set to time?

Cor
 
I

Ian Andrews

Works a treat now, thanks very much - Cant believe how close I had got to
what was required - Your help is greatly appreciated

Ian
 
I

Ian Andrews

Did think about this, but as I had over 400 entrys to be made, felt textbox
route was the easiest. This data is then written to file, and read on every
start up of this appilcation. Software to operate a marine aquarium, with
seasonal control of all lighting systems, tidal control over pumping systems
etc. Becomes very complex.

Thanks for your thoughts

Ian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top