?
Solved

Need RegEx to validate time entry

Posted on 2003-03-18
7
Medium Priority
?
165 Views
Last Modified: 2012-05-04
Hi - I need two visual basic functions, using regular expressions (or a better solution if appropriate?) one to validate that a string is a valid 24H format time value, the other to validate that a string is a valid 12H format time value.  Hours and Minutes, no seconds.

Thanks
-paul
0
Comment
Question by:PMH4514
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 14

Expert Comment

by:aelatik
ID: 8161771
I don't think thats possible, If i look at '11:00' i can't determine if its a 12 or 24 hour format unless it contains an AM or PM notification.

0
 

Author Comment

by:PMH4514
ID: 8161796
ok, lets assume the existance of AM or PM on the 12H string, and no such indicator on the 24H string..
0
 
LVL 4

Accepted Solution

by:
trkcorp earned 320 total points
ID: 8162043
The isdate function will return true or false on a time value as well as a date... For example:
Dim sTm as String
sTm = "11:00"
If IsDate(sTm) then
   MsgBox "Good, " & cDate(sTm) 'Returns "Good, 11:00:00 AM"
Else
   MsgBox "Time is invalid."
Endif
sTm = "13:00"
If IsDate(sTm) then
   MsgBox "Good, " & cDate(sTm) 'Returns "Good, 1:00:00 PM"
Else
   MsgBox "Time is invalid."
Endif
sTm = "1:30 PM"
If IsDate(sTm) then
   MsgBox "Good, " & cDate(sTm) 'Returns "Good, 1:30:00 PM"
Else
   MsgBox "Time is invalid."
Endif

Etceteras...
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 8162056
maybe:

Function ValidateHour(sHour As String) As Boolean
If InStr(1, sHour, "AM", vbTextCompare) Or InStr(1, sHour, "PM", vbTextCompare) Then
   ValidateHour = IsDate(sHour)
End If
End Function
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 8162066
ups!, sorry trkcorp
0
 

Author Comment

by:PMH4514
ID: 8162171
using the isDate() function to test a valid time will work..  doesn't specifically answer the question asked, but the end result for me is the same..
0
 
LVL 14

Expert Comment

by:aelatik
ID: 8162181
Just like Richie posted, If there is PM or AM in time then its a 12 hour notification, otherwise 24. Or am i wrong here ?
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses
Course of the Month8 days, 5 hours left to enroll

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question