[RESOLVED] Checking String Format
Hello. I am trying to write an alarm clock program with vb express 2008, but I've run into a problem that I need help with. I retrieve a time from a text box from the user, how do I make sure they have the correct format so I can match it up with a time that is using this format: Format(Now, "hh:mm.ss tt"). It seems simple, but it has me stumped right now. How do I tell if a part of a string is numeric or not? Any help would be appreciated.
Re: Checking String Format
First up, don't use this:
Code:
Format(Now, "hh:mm.ss tt")
Use this:
Code:
Date.Now.ToString("hh:mm.ss tt")
Secondly, don't use a TextBox. Use a DataTimePicker and then you don't have to do anything. You get a DateTime value from the DTP's Value property.
If you really did need to convert a string to a time, you'd use TimeSpan.TryParse, DateTime.TryParse or DateTime.TryParseExact.
Re: Checking String Format
you could use regex to validate it
vb Code:
Dim timeStr As String = "02:59.50 PM"
Dim rxTime As New Regex("^(0[1-9]:[0-5][0-9].[0-5][0-9]|1[0-2]:[0-5][0-9].[0-5][0-9]) (AM|PM)$")
MsgBox(rxTime.Match(timeStr).Success)
Re: Checking String Format
@brian - jmc said "If you really did need to convert a string to a time, you'd use TimeSpan.TryParse, DateTime.TryParse or DateTime.TryParseExact." They will all give valid / invalid indications of time.
BTW - i live in jefferson city.
Re: Checking String Format
Thanks for the responses, I think I have a solution to the input format problem, but I would still like to know a way to make sure part of a string is a numeric value. I would parse the string using the mid function, but what kind of check can I run on the parsed part to make sure it's a number value? Thanks for your help.
Re: Checking String Format
I would still suggest you to use the Jmc’s suggestion but if you want to check the string for numeric values than you need to write a method to do that. You could loop through the chars of the string and check if they are numeric or not.
Re: Checking String Format
Ok, but what do I compare them to? I mean what do I use to distinguish between numeric or non numeric?
Re: Checking String Format
Quote:
Originally Posted by Brian M.
Ok, but what do I compare them to? I mean what do I use to distinguish between numeric or non numeric?
The Char data type has a method “IsDigit” that returns a Boolean value that indicates if the character is digit or not.
Code:
Dim myString As String = "Text2"
For Each ch As Char In myString
If Char.IsDigit(ch) Then
'Is digit
Else
'Isn't digit
End If
Next
1 Attachment(s)
Re: Checking String Format
That's what I was looking for. Thanks alot. I just wanted a simple interface to wake me up in the morning. I have an alarm clock I made with just basic that uses wav files and random numbers, but its a console app, I wanted an alarm gui. Keep an eye out in code-bank for this alarm clock.
1 Attachment(s)
Re: Checking String Format
Quote:
Originally Posted by Brian M.
That's what I was looking for. Thanks alot. I just wanted a simple interface to wake me up in the morning. I have an alarm clock I made with just basic that uses wav files and random numbers, but its a console app, I wanted an alarm gui. Keep an eye out in code-bank for this alarm clock.
Looks nice, I have also started a desktop alarm clock and didn’t finish yet.
Re: [RESOLVED] Checking String Format
Its amazing how hard something simple like an alarm clock can be to make.
Re: [RESOLVED] Checking String Format
if you had used the datetimepicker for your input instead of a textbox it would have been easier.