pattern matching

Discussion in 'Javascript' started by Abby Lee, Sep 2, 2004.

  1. Abby Lee

    Abby Lee Guest

    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(pattern){
    if(pattern.value.match(/[0-9] + :[0-9] + [0-9] + [aApP] + [mM]/)) {
    alert ("works")
    }
    else alert(pattern.value + "Does not work")
    }
     
    Abby Lee, Sep 2, 2004
    #1
    1. Advertising

  2. Abby Lee

    Evertjan. Guest

    Abby Lee wrote on 02 sep 2004 in comp.lang.javascript:
    > 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(pattern){
    > if(pattern.value.match(/[0-9] + :[0-9] + [0-9] + [aApP] + [mM]/)) {
    > alert ("works")
    >}
    > else alert(pattern.value + "Does not work")
    >}
    >


    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(pattern){
    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)
     
    Evertjan., Sep 2, 2004
    #2
    1. Advertising

  3. JRS: In article <Xns9558C3721E891eejj99@194.109.133.29>, dated Thu, 2
    Sep 2004 17:12:43, seen in news:comp.lang.javascript, Evertjan.
    <> posted :
    >Abby Lee wrote on 02 sep 2004 in comp.lang.javascript:
    >> I ask the user to enter a time in the formatio 12:30 PM.


    >function checkTime(pattern){
    > if (/^\d{1,2}:\d{2}[ap]m$/i.test(pattern))

    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.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
    <URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
    <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
     
    Dr John Stockton, Sep 2, 2004
    #3
  4. Abby Lee

    Mick White Guest

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


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

    /^((0?[1-9])|(1[012])):[0-5]\d\s?[ap]m$/i
    Not tested.
    Mick
     
    Mick White, Sep 2, 2004
    #4
  5. Abby Lee

    Evertjan. Guest

    Mick White wrote on 02 sep 2004 in comp.lang.javascript:

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

    >
    > This would allow: 99:99Am .....
    >
    > /^((0?[1-9])|(1[012])):[0-5]\d\s?[ap]m$/i
    > Not tested.


    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)
     
    Evertjan., Sep 2, 2004
    #5
  6. Abby Lee

    Grant Wagner Guest

    Abby Lee wrote:

    > 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(pattern){
    > if(pattern.value.match(/[0-9] + :[0-9] + [0-9] + [aApP] + [mM]/)) {


    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.

    > alert ("works")
    > }
    > else alert(pattern.value + "Does not work")
    > }


    --
    Grant Wagner <>
    comp.lang.javascript FAQ - http://jibbering.com/faq
     
    Grant Wagner, Sep 8, 2004
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. DelphiDude
    Replies:
    3
    Views:
    1,170
  2. danpres2k
    Replies:
    3
    Views:
    7,483
    danpres2k
    Aug 25, 2003
  3. CV
    Replies:
    2
    Views:
    595
    Charles DeRykus
    Aug 31, 2004
  4. Marc Bissonnette

    Pattern matching : not matching problem

    Marc Bissonnette, Jan 8, 2004, in forum: Perl Misc
    Replies:
    9
    Views:
    239
    Marc Bissonnette
    Jan 13, 2004
  5. Bobby Chamness
    Replies:
    2
    Views:
    235
    Xicheng Jia
    May 3, 2007
Loading...

Share This Page