#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2005
    Posts
    390
    Rep Power
    10

    Defining a regex for mm/dd/yyyy


    I'm trying to tweak a piece of javascript that defines a regular expression for date formatting but I'm not sure if mine is correct. This is what I have:

    "/^[0-9]{1,2}[/][0-9]{1,2}[/][0-9]{4}$/"

    Does this look right?

    It's supposed to match mm/dd/yyyy date format
  2. #2
  3. Autodidact
    Devshed Novice (500 - 999 posts)

    Join Date
    Mar 2008
    Location
    Canada
    Posts
    741
    Rep Power
    833
    Advice: try it. You probably have by now. It's a bit too general. 00/00/0000 will return a true match, so will 99/99/9999. However, they're obviously not correct dates. You also don't need the square brackets around the forward slashes--escape them instead (or in the case of the code below, accept any character to delimit the date).

    Code:
    /^(0[1-9]{1}|[12]{1}[0-9]{1}|3[01]{1}).{1}(0[1-9]{1}|1[0-2]{1}).{1}([12]{1}[0-9]{3})$/
    This will accept 01-10-2009, 01/10/2009, or any other delimit character chosen. If you want it to be exactly the forward slash, replace the two parts where it's written .{1} with \/

    Just beware: If you plan to verify dates before the 11th century or after the 30th century, you'll need to modify it.
    Soldiers, from the height of these pyramids forty centuries look down upon you.
    ▪ Napoleon, on the eve of the Battle of the Pyramids
  4. #3
  5. CSS & JS/DOM Adept
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jul 2004
    Location
    USA (verifiably)
    Posts
    20,127
    Rep Power
    4304
    @f_nietzsche Is there a particular reason why you put so many occurrences of "{1}" in there?
    Spreading knowledge, one newbie at a time.

    Check out my blog. | Learn CSS. | PHP includes | X/HTML Validator | CSS validator | Common CSS Mistakes | Common JS Mistakes

    Remember people spend most of their time on other people's sites (so don't violate web design conventions).
  6. #4
  7. Autodidact
    Devshed Novice (500 - 999 posts)

    Join Date
    Mar 2008
    Location
    Canada
    Posts
    741
    Rep Power
    833
    Yeah--clarification. The more he knows of its workings, the sooner he'll be able to skip the longhand--kind of like math.
    Soldiers, from the height of these pyramids forty centuries look down upon you.
    ▪ Napoleon, on the eve of the Battle of the Pyramids
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2007
    Posts
    765
    Rep Power
    929
    Since your entire string is a date, wouldn't it be easier (and more accurate) to say !isNaN( Date.parse( input_string ) )?

    If you absolutely must have it in MM/DD/YYYY form you can always format the result of Date.parse.
    sub{*{$::{$_}}{CODE}==$_[0]&& print for(%:: )}->(\&Meh);
  10. #6
  11. CSS & JS/DOM Adept
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jul 2004
    Location
    USA (verifiably)
    Posts
    20,127
    Rep Power
    4304
    Originally Posted by OmegaZero
    Since your entire string is a date, wouldn't it be easier (and more accurate) to say !isNaN( Date.parse( input_string ) )?
    If I remember correctly, Date.parse() only supports certain separators in some browsers. I'm fairly certain that at least once I've had to use a regexp to change separators before calling Date.prase().
    Spreading knowledge, one newbie at a time.

    Check out my blog. | Learn CSS. | PHP includes | X/HTML Validator | CSS validator | Common CSS Mistakes | Common JS Mistakes

    Remember people spend most of their time on other people's sites (so don't violate web design conventions).

IMN logo majestic logo threadwatch logo seochat tools logo