Solved

Validate Time in VB.Net

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

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…
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
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 …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

813 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