Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to check if the input time is in correct format

Status
Not open for further replies.

geros1

Technical User
Jan 17, 2005
12
ES
hi! I would like my program to ask the user to enter a time in the form of hh:mm. How can I have it check if the input time is in the correct format and not eg. blank or 123 or something else? The input time is given in text1.text
and stored in a variable named datei.
Can anybody help?
thank you in advance

 
Dim SaveTime as string
SaveTime = Time
on error goto ErrHandler

Time = Text1.text

Exit sub
Time = SaveTime
End Sub

[gray]Experience is something you don't get until just after you need it.[/gray]
 
thank you but it doesn´t work.
you see i have a textbox called text1.text and i want the user to insert a future time (random) in the form of hh:mm eg. 16:36. when the user press a commandbox if the program detects a different format of the input value for example 16.36 or 1636 or 87263438 or kajshd or anything else then it should open a message box. (the 16:36 is just a random time, it can be anything, but only in this form: hh:mm)
thank you for your time
 
geros1,

The DatePicker (handles times too) control can be useful for this.

regards Hugh
 
geros1,

Put txtTimeHHMM textbox on the form and try this:

Private Sub txtTimeHHMM_Validate(Cancel As Boolean)

Dim strTimeSplit() As String

Cancel = InStr(1, txtTimeHHMM.Text, ":", vbBinaryCompare) = 0

If Not Cancel Then
Cancel = Not IsNumeric(Replace(txtTimeHHMM.Text, ":", "", 1, 1, vbBinaryCompare))

If Not Cancel Then
strTimeSplit = Split(txtTimeHHMM.Text, ":", , vbBinaryCompare)
Cancel = Val(strTimeSplit(0)) < 0 Or Val(strTimeSplit(0)) > 24
If Not Cancel Then
Cancel = Val(strTimeSplit(1)) < 0 Or Val(strTimeSplit(1)) > 60
End If
End If
End If

If Cancel Then
MsgBox "Please enter time in HH:MM format.", vbCritical
End If
End Sub

vladk
 
Your best bet is to use a regular expression to test the input value. First you need to tell the IDE to use the regular expressions lib:
[ul]
[li]Open your project[/li]
[li]Click on Project/References on the menu[/li]
[li]Scroll down until you find Miscrosoft VBScript Regular Expressions 5.5 and check it[/li]
[li]Click OK[/li]
[/ul]
Now for the actual code:
Code:
dim re
set re = new RegExp
re.Pattern = "^[0-2][0-9]:[0-5][0-9]$"
if re.Test(datei) then
  'time is valid
end if


Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
geros1,

Tracy's suggestion is fine. RegExp is usually way to go.
Should the pattern look like this?

re.Pattern = "^[0-2][0-4]:[0-5][0-9]$"

Regarding my code, it is better to replace

Cancel = InStr(1, txtTimeHHMM.Text, ":", vbBinaryCompare) = 0

with:

Cancel = InStr(1, txtTimeHHMM.Text, ":", vbBinaryCompare) = 0 Or Len(txtTimeHHMM.Text) <> 5

vladk
 
vladk: no, otherwise you couldn't enter "17:00" (quitting time).

geros1: note that this re is still not perfect. the user could enter 27:00 and it would be valid. Maybe:
Code:
re.Pattern = "^([01][0-9])|([2][0-4]):[0-5][0-9]$"
still not quite, since it allows "00:00", but you could test for that condition separately.


Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
geros1,

You could employ masked edit control:

Private Sub Form_Load()
MaskEdBox1.Mask = "##:##"
End Sub

Private Sub MaskEdBox1_Validate(Cancel As Boolean)
With MaskEdBox1
Cancel = Val(Left(.ClipText, 2)) > 24 Or _
Val(Right(.ClipText, 2)) > 59 Or _
Len(.ClipText) <> 4
End With
If Cancel Then
MsgBox "Please enter time in HH:MM format.", vbCritical
End If
End Sub
 

Thank you all for your time. it´s working...;)
 
I guess he didn't want to offend anyone by saying which method(s) he is or isn't using...

[smile]

Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
geros1

It does work. My example was just to demonstrate how to check the user input for validity. If you need to check the input without setting the system clock then just add a bit more code.

Dim SaveTime as string, MyTime as string
SaveTime = Time
on error goto ErrHandler

Time = Text1.text 'this line checks validity of user input

MyTime = Text1.text 'if valid then use it
Time = SaveTime 'and reset the system time back to SaveTime

Exit sub
ErrHandler: 'Sorry, I forgot this line in previous example
Time = SaveTime
End Sub

[gray]Experience is something you don't get until just after you need it.[/gray]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top