Solved

Validate Time in VB.Net

Posted on 2009-04-03
4
737 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
  • 2
  • 2
4 Comments
 
LVL 14

Accepted Solution

by:
jjardine earned 500 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Word Template Mail merge with vb.net 4 59
Events in static methods 3 50
vb.net 2 37
I need help converting a bitmap to an image in VB.Net 1 16
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 …
Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

862 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now