Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to verify if text is in time format?

Status
Not open for further replies.

markdmac

MIS
Dec 20, 2003
12,340
US
Hi VB.Net Experts,

Can anyone please provide me with some sample code to verify if text entered into a text box is in the format of 00:00:00.

I assume I should be using Regular Expressions but have never been able to master those.

I need to prompt the user to enter the correct number of minutes if the input is not in the proper time format.

From my research I think I need to use the following to validate the format:
^([0-1]\d|2[0-3]):([0-5]\d):)([0-5]\d))?$


However I am not sure how to add this to my code. I am working off of the OnClick action for a button and it needs to validate tbPolicyName.Text as the desired format.

Any assistance with sample code is greatly appreciated.
 
I managed to get this working. For anyone following me, here is what I needed.

First in my buttons On Click I added the following:
Code:
If IsValid_Time(tbPolicyName.Text) = False Then
                        MsgBox("Enter time in Hours: Minutes: Seconds format. (ex:00:30:00)")
                        Exit Select
                    End If

Also at the very top of my code I needed to do an import.
Code:
Imports System.Text.RegularExpressions

And lastly I created a function to check against (returns true or false for match)

Code:
    Function IsValid_Time(ByVal sTime As String)
        Return Regex.IsMatch(sTime, "^([0-1]\d|2[0-3]):([0-5]\d)(:([0-5]\d))?$")
    End Function

I hope that helps.

Regards,

Mark

Check out my scripting solutions at
Work SMARTER not HARDER. The Spider's Parlor's Admin Script Pack is a collection of Administrative scripts designed to make IT Administration easier! Save time, get more work done, get the Admin Script Pack.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top