Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Senior Coder
    Join Date
    Jun 2002
    Location
    41° 8' 52" N -95° 53' 31" W
    Posts
    3,660
    Thanks
    0
    Thanked 0 Times in 0 Posts

    JavaScript Date Validation

    Recently while messing with dates, I noticed an odd quirk in javascript with new Date(), i.e. if someone enters an invalid date, such as 2/29/2003, javascript creates the new date as 3/1/2003.

    Having a look around, I couldn't find any scripts that took advantage of this for the sake of date validation... probably someone here has done this before, but I'll post it anyway.

    The idea is that if javascript creates a new Date() with a different month, then obviously the date entered is not valid. Most of the scripts I saw used some math to divide by leap year, yadda yadda yadda, but with this feature (?) of javascript, it seems unnecessary.

    Right now this code only validates mm/dd/yyyy, but it should be easy to modify to support other formats:

    Code:
    function isDate(sDate) {
       var re = /^\d{1,2}\/\d{1,2}\/\d{4}$/
       if (re.test(sDate)) {
          var dArr = sDate.split("/");
          var d = new Date(sDate);
          return d.getMonth() + 1 == dArr[0] && d.getDate() == dArr[1] && d.getFullYear() == dArr[2];
       }
       else {
          return false;
       }
    }
    Here's a shorter version that works if you pass in the values separately:

    Code:
    function isDate(mm,dd,yyyy) {
       var d = new Date(mm + "/" + dd + "/" + yyyy);
       return d.getMonth() + 1 == mm && d.getDate() == dd && d.getFullYear() == yyyy;
    }
    Comments/modifications?
    Last edited by whammy; 02-09-2003 at 12:11 AM.
    Former ASP Forum Moderator - I'm back!

    If you can teach yourself how to learn, you can learn anything. ;)

  • #2
    Regular Coder
    Join Date
    Dec 2002
    Posts
    169
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: JavaScript Date Validation

    Funny you should mention that! I use this fact to my advantage in my fully-navigable cross-browser pop-up calendar...

    Originally, I developed this calendar because I wanted an intuitive and convenient way for users to enter valid dates. To keep it friendly, I allow the user to select year, month and day in any order, and rebuild its internal date accordingly. From March 29, 2003, if you select February, the calendar shows you the 29th day of February 2003 (namely, March 1st!).

    Some might call that a bug, but what else should I assume?

    Originally posted by whammy
    Recently while messing with dates, I noticed an odd quirk in javascript with new Date(), i.e. if someone enters an invalid date, such as 2/29/2003, javascript creates the new date as 3/1/2003.

    Having a look around, I couldn't find any scripts that took advantage of this for the sake of date validation... probably someone here has done this before, but I'll post it anyway.

  • #3
    Moderator
    Join Date
    May 2002
    Location
    Hayward, CA
    Posts
    1,461
    Thanks
    1
    Thanked 23 Times in 21 Posts
    That quirk is a result of how the Date constructor is defined in ECMAScript, 3rd Edition.
    "The first step to confirming there is a bug in someone else's work is confirming there are no bugs in your own."
    June 30, 2001
    author, Verbosio prototype XML Editor
    author, JavaScript Developer's Dictionary
    https://alexvincent.us/blog

  • #4
    Senior Coder
    Join Date
    Jun 2002
    Location
    41° 8' 52" N -95° 53' 31" W
    Posts
    3,660
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Whatever it is, I like it!

    Alex, how far back do you think this will be compatible, browser-wise?
    Last edited by whammy; 02-08-2003 at 11:46 PM.
    Former ASP Forum Moderator - I'm back!

    If you can teach yourself how to learn, you can learn anything. ;)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •