homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

Regular expression help needed
Date matching

5+ Year Member

Msg#: 3743695 posted 4:57 pm on Sep 12, 2008 (gmt 0)


I'm pretty new to regular expressions and need a little help figuring out why my expression seems to be incorrectly evaluated.

My scenario: I need to test a value and see if it's in the format ##/##/##. It's a date (month/day/year), but I can't get the expression to return false. In other words every value is returning true, even "On shelf", which obviously shouldn't.

Here's my javascript:
if (duedate.match(/((\d{2})\/)*/)) {

The idea is to look for the pattern ##/ repeating any number of times.

What am I missing?

Thanks for you help!




WebmasterWorld Senior Member drdoc us a WebmasterWorld Top Contributor of All Time 10+ Year Member

Msg#: 3743695 posted 5:12 pm on Sep 12, 2008 (gmt 0)

Well, your pattern would match something like:
(In fact, it would even match "foo 99/ bar".)

But not:

Do you see the problem? Your pattern requires a trailing slash.

However, I would improve the pattern to explicitly look for a string in the format #/##/## or ##/##/## thus:

If you want to get it even better, you should probably do something like this:

Remember to replace the broken bar ¦ with an actual vertical bar.

Of course, invalid dates like 2/30/08 would still be allowed through. But that will be handled by your server side validation.


5+ Year Member

Msg#: 3743695 posted 6:20 pm on Sep 12, 2008 (gmt 0)

Thanks for the reply and help.

Unfortunately, I'm getting an error in Firebug about an "unterminated regular expression literal".

Here's the updated code:
if (duedate.match(/^\d{1,2}\/\d{1,2}\/\d{2}$/)) {

Maybe my eyes are going bad. What could be unterminated?

Thanks again,


5+ Year Member

Msg#: 3743695 posted 4:42 pm on Sep 17, 2008 (gmt 0)


Fixed it. Here's what worked for me:

var duedatematch = /^\d{1,2}\/\d{1,2}\/\d{2}/ <== There is no semi-colon

if (duedatematch.test(duedate)){

For some reason, when the regexp was inside parens in the earlier suggestion, that final "/" in the pattern was apparently somehow causing the one of the enclosing parens to be escaped(!), thus the "unterminated" javascript error was generated. No idea how that could be possible since "\" is the escape character!


Anyway, I hope this helps someone else.


Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved