Solved

Validate Time in VB.Net

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Open a link in vb.net 2 16
ASP.NET (VB) return a record 2 38
Name Space error VS2015 1 24
Generate Unique ID in VB.NET 21 61
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
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 video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

861 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