By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,504 Members | 1,110 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,504 IT Pros & Developers. It's quick & easy.

pattern matching

P: n/a
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")
}
Jul 23 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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)

Jul 23 '05 #2

P: n/a
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
Jul 23 '05 #3

P: n/a
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)

Jul 23 '05 #4

P: n/a
JRS: In article <Xn********************@194.109.133.29>, dated Thu, 2
Sep 2004 17:12:43, seen in news:comp.lang.javascript, Evertjan.
<ex**************@interxnl.net> 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.
Jul 23 '05 #5

P: n/a


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 <gw*****@agricoreunited.com>
comp.lang.javascript FAQ - http://jibbering.com/faq

Jul 23 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.