pattern matching

A

Abby Lee

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")
}
 
E

Evertjan.

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?
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated Thu, 2
Sep 2004 17:12:43, seen in Evertjan.
Abby Lee wrote on 02 sep 2004 in comp.lang.javascript:
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.
 
M

Mick White

Evertjan. said:
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
 
E

Evertjan.

Mick White wrote on 02 sep 2004 in comp.lang.javascript:
Evertjan. said:
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.
 
G

Grant Wagner

Abby said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,045
Latest member
DRCM

Latest Threads

Top