Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Validate Time in VB.Net

Posted on 2009-04-03
4
Medium Priority
?
745 Views
Last Modified: 2012-08-14
I'm using the solution I found here: http://www.experts-exchange.com/Programming/Languages/.NET/Visual_Basic.NET/Q_23794351.html?sfQueryTermInfo=1+10+time+valid+vb.net

I appears to be working, except that when you enter a time such as 13:303 the time is not caught.
Public Function IsValidTime(ByVal TheTime As String) As Boolean
        ' Format 1 validates the time against the 12 hour format
        Dim Format1 As String = "^ *(1[0-2]|[1-9]):[0-5][0-9] *(a|p|A|P)(m|M) *$"
        Dim TryValidateFormat1 As New System.Text.RegularExpressions.Regex(Format1)
        ' Format 2 validates the 24 hour format
        Dim Format2 As String = "([0-1][0-9]|2[0-3]):([0-5][0-9])"
        Dim TryValidateFormat2 As New System.Text.RegularExpressions.Regex(Format2)
 
        Return TryValidateFormat1.IsMatch(TheTime) Or TryValidateFormat2.IsMatch(TheTime)
 
    End Function

Open in new window

0
Comment
Question by:Cahl
[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
4 Comments
 
LVL 14

Accepted Solution

by:
jjardine earned 2000 total points
ID: 24063059
What if you add the $ symbol to the end of your 2nd regular expression?   That should signify that the statement should end there. see the first expression for an example.
0
 

Author Comment

by:Cahl
ID: 24063502
I saw that, but in the orignal post, the author of the post said that it returning false on valid times. The solution provider instructed him to take out the "$" which then allowed it to work correctly.  I just tried it with it in and now sees valid times and invalid. I took the space out between the last ) and the $ and it seems to work.

Should the space be removed from the first regular expression as well?
Returns incorrectly
Dim Format2 As String = "([0-1][0-9]|2[0-3]):([0-5][0-9]) $"
Returns Correctly
Dim Format2 As String = "([0-1][0-9]|2[0-3]):([0-5][0-9])$"
 
Clear space out?
Dim Format1 As String = "^ *(1[0-2]|[1-9]):[0-5][0-9] *(a|p|A|P)(m|M) *$"

Open in new window

0
 
LVL 14

Expert Comment

by:jjardine
ID: 24065822
I don't think it matters in the first one because a space shouldn't make a difference after the am or pm.  
0
 

Author Comment

by:Cahl
ID: 24078876
ok, Thanks!
0

Featured Post

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

618 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