Results 1 to 3 of 3

Thread: checking for a date value

  1. #1
    Guest

    Talking

    Can anyone tell me if, and if so, how I can check if a combination of dd, mm and yyyy is a valid date?

    Thanx!

  2. #2
    Addicted Member ShIzO's Avatar
    Join Date
    Apr 1999
    Location
    Bartlett, IL
    Posts
    189
    Code:
    <script language='JavaScript'>
    function checkDate() {
      var myDate = document.[formName].[textfieldname].value;
      // assuming that date format is MM/DD/YYYY
      var month = mydate.substring(0,2);
      var day = mydate.substring(3,5);
      var year = mydate.substring(6,10)
    
      //then you perform checks
      if (!((month > 0) && (month < 13))) {
         alert("Month must be 1-12!");
         reuturn false;
      }
    
      //
      // then for day and year
    
    }
    call that function upon submiting the form or whatever
    www.HardFind.com -buy/sell/trade your used hardware.

  3. #3
    Guest
    Thanx, that's very nice, but it doesn't check wether or I enter November 31st as a date, for instance. I've written the whole function myself now, and it works, but I was just wondering if maybe there was a stadard function for it, but I guess the answer to that question is now, huh?

    Thanx anyway!

    Oh, in case anyone is interested, here's the code I came up with (granted: it doesn't check if the year is divisable by 100 or 400, but then again, if the code is still running in 2100, I'll come and adjust it for you personally) :

    BTW, please pay no intention to the lack of indentation.. for some reason it won't work for me..

    -----------------------------------------------------

    function IsValidDate(myday, mymonth, myyear)
    {
    if ((myday < 1) || (myday > 31))
    {
    return false;
    }
    if ((mymonth < 1) || (mymonth > 12))
    {
    return false;
    }
    if (myyear < 2000)
    {
    return false;
    }
    switch (mymonth)
    {
    case "2" :
    if (!((myday <= 28) || ((myday <= 29) && ((myyear % 4) = 0))))
    {
    return false;
    }
    case "4" :
    if (myday > 30)
    {
    return false;
    }
    case "6" :
    if (myday > 30)
    {
    return false;
    }
    case "9" :
    if (myday > 30)
    {
    return false;
    }
    case "11" :
    if (myday > 30)
    {
    return false;
    }
    }
    return true;
    }

    -----------------------------------------------------

Posting Permissions

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



Click Here to Expand Forum to Full Width