Solved

Validate Time in VB.Net

Posted on 2009-04-03
4
736 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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

747 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

14 Experts available now in Live!

Get 1:1 Help Now