Results 1 to 12 of 12

Thread: [RESOLVED] Checking String Format

  1. #1

    Thread Starter
    Lively Member Brian M.'s Avatar
    Join Date
    Nov 2006
    Location
    Moberly MO
    Posts
    94

    Resolved [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.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    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.

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: Checking String Format

    you could use regex to validate it

    vb Code:
    1. Dim timeStr As String = "02:59.50 PM"
    2. 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)$")
    3. MsgBox(rxTime.Match(timeStr).Success)
    Last edited by .paul.; Jul 13th, 2008 at 08:54 AM.

  4. #4
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,764

    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.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  5. #5

    Thread Starter
    Lively Member Brian M.'s Avatar
    Join Date
    Nov 2006
    Location
    Moberly MO
    Posts
    94

    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.

  6. #6
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    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.

  7. #7

    Thread Starter
    Lively Member Brian M.'s Avatar
    Join Date
    Nov 2006
    Location
    Moberly MO
    Posts
    94

    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?

  8. #8
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    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

  9. #9

    Thread Starter
    Lively Member Brian M.'s Avatar
    Join Date
    Nov 2006
    Location
    Moberly MO
    Posts
    94

    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.
    Attached Images Attached Images  

  10. #10
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    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.
    Attached Images Attached Images  

  11. #11

    Thread Starter
    Lively Member Brian M.'s Avatar
    Join Date
    Nov 2006
    Location
    Moberly MO
    Posts
    94

    Re: [RESOLVED] Checking String Format

    Its amazing how hard something simple like an alarm clock can be to make.

  12. #12
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,764

    Re: [RESOLVED] Checking String Format

    if you had used the datetimepicker for your input instead of a textbox it would have been easier.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width