RegExpression Help

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jim in Arizona

    #1

    RegExpression Help

    I set up a regular expression validator on my web form to make sure
    users were putting in correct time format but I can't seem to get it right.

    The problem is that I need them to input a time based on the 12 hour
    clock. This regular expression works fine:

    ^([0-1][0-2]):([0-5][0-9])$

    However, this does not include AM or PM designators and this is where I
    can't seem to find the proper expression. I have tried this:

    ^([0-1][0-2]):([0-5][0-9]):b([AM]|[am]|[PM]|[pm])$

    That one doesn't work. I even tried making it as simple as possible just
    for testing purposes, like so:

    ^([0-1][0-2]):([0-5][0-9])([AM])$
    or
    ^([0-1][0-2]):([0-5][0-9])AM$

    But, when I put in a time like 12:20AM or 12:20 AM, the validator is
    triggered.

    I'm hoping to avoid using a 24 hour clock as this would just cause
    frustration for our secretaries.

    TIA,
    Jim
  • Alexey Smirnov

    #2
    Re: RegExpression Help

    On Apr 2, 7:03 pm, Jim in Arizona <tiltow...@hotm ail.comwrote:
    I set up a regular expression validator on my web form to make sure
    users were putting in correct time format but I can't seem to get it right.
    >
    The problem is that I need them to input a time based on the 12 hour
    clock. This regular expression works fine:
    >
    ^([0-1][0-2]):([0-5][0-9])$
    >
    However, this does not include AM or PM designators and this is where I
    can't seem to find the proper expression. I have tried this:
    >
    ^([0-1][0-2]):([0-5][0-9]):b([AM]|[am]|[PM]|[pm])$
    >
    That one doesn't work. I even tried making it as simple as possible just
    for testing purposes, like so:
    >
    ^([0-1][0-2]):([0-5][0-9])([AM])$
    or
    ^([0-1][0-2]):([0-5][0-9])AM$
    >
    But, when I put in a time like 12:20AM or 12:20 AM, the validator is
    triggered.
    >
    I'm hoping to avoid using a 24 hour clock as this would just cause
    frustration for our secretaries.
    >
    TIA,
    Jim
    Jim, you don't need brackets (used to create a set of characters) and
    because of single space you have to use a "\s"

    This will work

    ^([0-1][0-2]):([0-5][0-9]\s?AM|am|PM|pm) $

    Comment

    • Jim in Arizona

      #3
      Re: RegExpression Help

      Jim, you don't need brackets (used to create a set of characters) and
      because of single space you have to use a "\s"
      >
      This will work
      >
      ^([0-1][0-2]):([0-5][0-9]\s?AM|am|PM|pm) $
      >

      I'm pretty new at regular expressions being that this is my first time
      ever trying to use them. I was going off the msdn article at
      http://msdn2.microsoft.com/en-us/lib...cs(VS.80).aspx and the :b
      was supposed to be a single space. Of course, I was using it wrong.

      Thanks for your help. I'll be able to move forward with my project now.

      Comment

      • Cowboy \(Gregory A. Beamer\)

        #4
        Re: RegExpression Help

        ^(((([1-9])|(([0][0-9])|([1][0-2]))):[0-5][0-9])\s?(AM|PM)?)|( ([0-9])|([0-1][0-9])|([2][0-3])):([0-5][0-9])$

        Tests the following as true

        12 hour time
        1:00
        12:59
        1:01 AM
        1:01 PM
        12:59 AM
        12:59 PM

        24 hour time
        00:01 - 12:01 AM
        23:59 - 11:59 PM

        Will fail on AM/PM on any 24 hour designation (anything 13:00 and above).
        Have not fully tested all varieties.

        You know you can also test valid times like this:

        public static bool isValidTime(str ing time, ref DateTime dt)
        {
        return DateTime.TryPar se(time, out dt);
        }



        --
        Gregory A. Beamer
        MVP; MCP: +I, SE, SD, DBA


        *************** *************** ***************
        Think outside the box!
        *************** *************** ***************
        "Jim in Arizona" <tiltowait@hotm ail.comwrote in message
        news:O6MxciUdHH A.3632@TK2MSFTN GP02.phx.gbl...
        >I set up a regular expression validator on my web form to make sure users
        >were putting in correct time format but I can't seem to get it right.
        >
        The problem is that I need them to input a time based on the 12 hour
        clock. This regular expression works fine:
        >
        ^([0-1][0-2]):([0-5][0-9])$
        >
        However, this does not include AM or PM designators and this is where I
        can't seem to find the proper expression. I have tried this:
        >
        ^([0-1][0-2]):([0-5][0-9]):b([AM]|[am]|[PM]|[pm])$
        >
        That one doesn't work. I even tried making it as simple as possible just
        for testing purposes, like so:
        >
        ^([0-1][0-2]):([0-5][0-9])([AM])$
        or
        ^([0-1][0-2]):([0-5][0-9])AM$
        >
        But, when I put in a time like 12:20AM or 12:20 AM, the validator is
        triggered.
        >
        I'm hoping to avoid using a 24 hour clock as this would just cause
        frustration for our secretaries.
        >
        TIA,
        Jim

        Comment

        • Alexey Smirnov

          #5
          Re: RegExpression Help

          On Apr 2, 9:46 pm, Jim in Arizona <tiltow...@hotm ail.comwrote:
          Jim, you don't need brackets (used to create a set of characters) and
          because of single space you have to use a "\s"
          >
          This will work
          >
          ^([0-1][0-2]):([0-5][0-9]\s?AM|am|PM|pm) $
          >
          I'm pretty new at regular expressions being that this is my first time
          ever trying to use them. I was going off the msdn article athttp://msdn2.microsoft .com/en-us/library/2k3te2cs(VS.80) .aspxand the :b
          was supposed to be a single space. Of course, I was using it wrong.
          >
          Jim, that article is about a regular expressions used in the Visual
          Studio's "Find and Replace Window" :-)

          You need to look at .NET Regular Expressions,

          for example here
          http://msdn2.microsoft.com/en-us/lib...3z(VS.71).aspx

          Comment

          • Jim in Arizona

            #6
            Re: RegExpression Help

            Alexey Smirnov wrote:
            On Apr 2, 9:46 pm, Jim in Arizona <tiltow...@hotm ail.comwrote:
            >>Jim, you don't need brackets (used to create a set of characters) and
            >>because of single space you have to use a "\s"
            >>This will work
            >>^([0-1][0-2]):([0-5][0-9]\s?AM|am|PM|pm) $
            >I'm pretty new at regular expressions being that this is my first time
            >ever trying to use them. I was going off the msdn article athttp://msdn2.microsoft .com/en-us/library/2k3te2cs(VS.80) .aspxand the :b
            >was supposed to be a single space. Of course, I was using it wrong.
            >>
            >
            Jim, that article is about a regular expressions used in the Visual
            Studio's "Find and Replace Window" :-)
            >
            You need to look at .NET Regular Expressions,
            >
            for example here
            http://msdn2.microsoft.com/en-us/lib...3z(VS.71).aspx
            >
            Well. That makes a world of difference, huh?
            Thanks again.

            Comment

            Working...