pattern matching

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Abby Lee

    #1

    pattern matching

    I ask the user to enter a time in the formatio 12:30 PM. onChange I
    send the string to this function. I'm using alert boxes to test
    it...and am always getting the "Does not work" alert box. What am I
    doing wrong?

    function checkTime(patte rn){
    if(pattern.valu e.match(/[0-9] + :[0-9] + [0-9] + [aApP] + [mM]/)) {
    alert ("works")
    }
    else alert(pattern.v alue + "Does not work")
    }
  • Evertjan.

    #2
    Re: pattern matching

    Abby Lee wrote on 02 sep 2004 in comp.lang.javas cript:[color=blue]
    > I ask the user to enter a time in the formatio 12:30 PM. onChange I
    > send the string to this function. I'm using alert boxes to test
    > it...and am always getting the "Does not work" alert box. What am I
    > doing wrong?
    >
    > function checkTime(patte rn){
    > if(pattern.valu e.match(/[0-9] + :[0-9] + [0-9] + [aApP] + [mM]/)) {
    > alert ("works")
    >}
    > else alert(pattern.v alue + "Does not work")
    >}
    >[/color]

    Use test, it gives true or false.

    You have to read up on regex syntax,
    the + is not what you think.

    "Does not work" works like a red screen on a bull in this NG.

    Why pattern.value?


    function checkTime(patte rn){
    if (/^\d{1,2}:\d{2}[ap]m$/i.test(pattern) )
    alert ("correct")
    else
    alert(pattern + " is incorrect")
    }

    btw, why not have your users use a 24 hour clock?


    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress,
    but let us keep the discussions in the newsgroup)

    Comment

    • Mick White

      #3
      Re: pattern matching

      Evertjan. wrote:[color=blue]
      >
      > function checkTime(patte rn){
      > if (/^\d{1,2}:\d{2}[ap]m$/i.test(pattern) )
      > alert ("correct")
      > else
      > alert(pattern + " is incorrect")
      > }[/color]

      This would allow: 99:99Am .....

      /^((0?[1-9])|(1[012])):[0-5]\d\s?[ap]m$/i
      Not tested.
      Mick

      Comment

      • Evertjan.

        #4
        Re: pattern matching

        Mick White wrote on 02 sep 2004 in comp.lang.javas cript:
        [color=blue]
        > Evertjan. wrote:[color=green]
        >>
        >> function checkTime(patte rn){
        >> if (/^\d{1,2}:\d{2}[ap]m$/i.test(pattern) )
        >> alert ("correct")
        >> else
        >> alert(pattern + " is incorrect")
        >> }[/color]
        >
        > This would allow: 99:99Am .....
        >
        > /^((0?[1-9])|(1[012])):[0-5]\d\s?[ap]m$/i
        > Not tested.[/color]

        Sure, but there is a difference between correcting the OP's syntax
        and improving on his idea/specs.


        --
        Evertjan.
        The Netherlands.
        (Please change the x'es to dots in my emailaddress,
        but let us keep the discussions in the newsgroup)

        Comment

        • Dr John Stockton

          #5
          Re: pattern matching

          JRS: In article <Xns9558C3721E8 91eejj99@194.10 9.133.29>, dated Thu, 2
          Sep 2004 17:12:43, seen in news:comp.lang. javascript, Evertjan.
          <exjxw.hannivoo rt@interxnl.net > posted :[color=blue]
          >Abby Lee wrote on 02 sep 2004 in comp.lang.javas cript:[color=green]
          >> I ask the user to enter a time in the formatio 12:30 PM.[/color][/color]
          [color=blue]
          >function checkTime(patte rn){
          > if (/^\d{1,2}:\d{2}[ap]m$/i.test(pattern) )[/color]
          if (/^\d{1,2}:\d{2} [ap]m$/i.test(pattern) ) // to be exact
          if (/^[01]?\d:\d{2} [ap]m$/i.test(pattern) ) // hours 0-9,00-19
          if (/^([1-9]|1[012]):\d{2} [ap]m$/i.test(pattern) ) // hours 1-12

          The minutes field can be checked with, instead of \d{2}, [0-5]\d - then
          we have an expression that allows only well-formed alien times (ISTM).

          In the last form above, one can insert either 0 or 0? before the first [
          ..

          Part-tested.

          --
          © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
          <URL:http://www.jibbering.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
          <URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
          <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

          Comment

          • Grant Wagner

            #6
            Re: pattern matching



            Abby Lee wrote:
            [color=blue]
            > I ask the user to enter a time in the formatio 12:30 PM. onChange I
            > send the string to this function. I'm using alert boxes to test
            > it...and am always getting the "Does not work" alert box. What am I
            > doing wrong?
            >
            > function checkTime(patte rn){
            > if(pattern.valu e.match(/[0-9] + :[0-9] + [0-9] + [aApP] + [mM]/)) {[/color]

            This regular expression matches:

            - a single digit /[0-9]/
            - one or more spaces / +/
            - a space / /
            - a colon /:/
            - a single digit /[0-9]/
            - one or more spaces / +/
            - a space / /
            - the letters a, p, A, or P /[aApP]/
            - one or more spaces / +/
            - a space / /
            - the letters m or M /[mM]/

            Is this really what you want?

            I'm guessing you think the "+"s are concatenation or something, used to
            "assemble" the regular expression. They aren't required, and in fact,
            the "+" has a special meaning in regular expression syntax, so it
            doesn't even match a "+" character.

            You probably want something like:

            if (/^\s*(1[012]|[1-9]):[0-5][0-9] ?[ap]m/i.test(pattern. value)) {

            This matches (case-insensitively):

            - from the beginning of the line /^/
            - zero or more whitespace /\s*/ - accounts for any spaces the user
            might enter
            - the digit "1" /1/ followed by the digits 0, 1 or 2 [012]; OR
            - the digits 1 to 9 /[1-9]/
            - a colon /:/
            - the digits 0 to 5 /[0-5]/
            - the digits 0 to 9 /[0-9]/
            - an optional space / ?/ - in case your users don't enter one
            - the letters a or p /[ap]/
            - the letter m /m/

            It should match:

            1:00am
            12:30 PM
            4:37 Am

            I haven't tested it thoroughly, but it seems to do what you want.
            [color=blue]
            > alert ("works")
            > }
            > else alert(pattern.v alue + "Does not work")
            > }[/color]

            --
            Grant Wagner <gwagner@agrico reunited.com>
            comp.lang.javas cript FAQ - http://jibbering.com/faq

            Comment

            Working...