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 8 of 8
  1. #1
    New to the CF scene
    Join Date
    May 2010
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    validating date range

    I want to validate two a date range using javascript, so that my program would check startdate less than end date.

    The below function only check the year, and it works.





    Code:
    function validate()
    {
    
    var start=document.entry.datum1.value;
    year = start.substr(0, 4);
    month = start.substr(5, 2);
    dayval = start.substr(8, 2);
    
    var end=document.entry.datum2.value;
    year1 = end.substr(0, 4);
    month1 = end.substr(5, 2);
    dayval1 = end.substr(8, 2);
    
    
    var myDate=new Date();
    myDate.setFullYear(year,month,dayval);
    var today = new Date();
    today.setFullYear(year1,month1,dayval1);
    
    if (myDate> today)
      {
      alert("The start date must come before the end date");
      return false
      }
    
    return true;
    }
    But I have write a function for checking the whole date value and it didnt work.
    My date is in format-
    2008-10-23(yyyy-mm-dd)

    Code:
    function validate()
    {
    var start=entry.datum1.value;
    year = start.substr(0, 4);
    month = start.substr(5, 2);
    dayval = start.substr(8, 2);
    
    intyear = parseInt(year, 10);
    intmonth = parseInt(month, 10);
    intdayval = parseInt(dayval, 10);
    
    var end=entry.datum2.value;
    year1 = end.substr(0, 4);
    month1 = end.substr(5, 2);
    dayval1 = end.substr(8, 2);
    
    intyear1 = parseInt(year1, 10);
    intmonth1 = parseInt(month1, 10);
    intdayval1 = parseInt(dayval1, 10);
    
    var startDate = Date.parse(intyear,intdayval,intmonth);
       var endDate = Date.parse(intyear1,intdayval1,intmonth1);
    
    if (startDate > endDate)
      {
      alert("The start date must come before the end date");
      return false;
      }
    return true;
    
    }
    Please help

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,011
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    Try this:-

    Code:
    <script type = "text/javascript">
    
    function checkDate() {
    var firstDate = new Date(2009,7,31);  // YYYY,MM(0-11),DD
    // remember that in Javascript date objects the months are 0-11
    var secondDate = new Date(2009,8,1); // should pass
    //var secondDate = new Date(2009,7,16); // should fail
    
    if (firstDate > secondDate) { 
    alert ("Invalid - The first date is after the second date");
    return false;
    } 
    else {
    alert ("Valid - The second date is after the first date");
    return true;
    } 
    
    }
    
    checkDate();
    </script>

    If you want to check that the date is a valid date (e.g. not 31st February) you can use:-

    Code:
    <script type = "text/javascript">
    
    function checkValidDate(yr,mmx,dd) {
    
    mm = mmx-1;  // remember that in Javascript date objects the months are 0-11
    var nd = new Date();
    nd.setFullYear(yr,mm,dd);  // format YYYY,MM(0-11),DD
    
    var ndmm = nd.getMonth();
    if (ndmm != mm) {
    alert (dd + "/" + mmx + "/" + yr  + " is an Invalid Date!");
    return false; 
    }
    else {
    alert (dd + "/" + mmx + "/" + yr  + " is a Valid Date");
    }
    }
    
    checkValidDate(2010,2,31)  // 31st February 2010
    </script>

    This is the steepest part of the course and I am afraid that it gets steeper later on. - Sports commentator
    Last edited by Philip M; 05-24-2010 at 04:13 PM. Reason: typo

  • #3
    New to the CF scene
    Join Date
    May 2010
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hello,
    thank you for your reply.
    But I am accepting the date from a textbox, in the input page.
    So I am getting a date in format.

    Code:
    var start=entry.datum1.value;
    year = start.substr(0, 4);
    month = start.substr(5, 2);
    dayval = start.substr(8, 2);
    start=2009-09-06
    as u have mentioned javascript accept month in format 0-11, then how can I convert myinput month to the required java script month format.

    Thank you

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,011
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    Quote Originally Posted by newjs View Post
    Hello,
    thank you for your reply.
    But I am accepting the date from a textbox, in the input page.
    So I am getting a date in format.

    Code:
    var start=entry.datum1.value;
    year = start.substr(0, 4);
    month = start.substr(5, 2);
    dayval = start.substr(8, 2);
    start=2009-09-06
    as u have mentioned javascript accept month in format 0-11, then how can I convert myinput month to the required java script month format.

    Thank you
    Well, surely you can adapt the date format to suit: e.g.

    var start = "2009-09-06"; // 6th September 2009
    start = start.replace(/-|:|\s/g,"/"); // allow slashes colons and spaces as well as hyphens as seperators
    var s = start.split("/"); // note this accepts 2009-9-6 or 2009/9/6 as well
    then yr = s[0]*1, mm = s[1]*1, dd = s[2]*1 (use *1 to change string values to numbers)

    and if necessary adjust for Javascript months:-

    mmx - mm-1;
    Last edited by Philip M; 05-24-2010 at 04:53 PM.

  • #5
    New to the CF scene
    Join Date
    May 2010
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    sorry
    Last edited by newjs; 05-24-2010 at 04:56 PM.

  • #6
    New to the CF scene
    Join Date
    May 2010
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Sorry, the code is still not working for month, it only checks year.

    Code:
    var start=document.entry.datum1.value;
    var s = start.split("-"); // note this accepts 2009-9-6 as well
    yr = s[0], mm = s[1], dd = s[2];
    mmx=mm-1;
    
    
    
    var end=document.entry.datum2.value;
    var s1 = end.split("-");
    yr1 = s1[0], mm1 = s1[1], dd1 = s1[2];
    mmx1=mm1-1;
    
    var firstDate = new Date(yr,mmx,dd);
    var secondDate = new Date(yr1,mmx1,dd1); // should pass
    
    
    
    
    if (firstDate > secondDate) {
    alert ("Invalid - The start date must come before the end date");
    return false;

  • #7
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,011
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    Code:
    <form name = "entry">
    Start Date <input type = "text" name = "datum1"><br>
    End Date <input type = "text" name = "datum2"><br>
    <input type = "button" value = "Check The Dates" onclick = "checkDates()">
    </form>
    
    <script type = "text/javascript">
    
    function checkDates() {
    var start=document.entry.datum1.value;
    var s = start.split("-"); // note this accepts 2009-9-6 as well
    yr = s[0];
    mm = s[1];
    dd = s[2];
    mmx = mm-1;
    
    var end=document.entry.datum2.value;
    var s1 = end.split("-");
    yr1 = s1[0];
    mm1 = s1[1];
    dd1 = s1[2];
    mmx1 = mm1-1;
    
    var firstDate = new Date(yr,mmx,dd);
    var secondDate = new Date(yr1,mmx1,dd1); // should pass
    
    if (firstDate >= secondDate) {
    alert ("Invalid - The start date must come before the end date");
    return false;
    }
    else {
    alert ("OK");
    }
    
    }
    
    </script>
    You need to check at some stage that the dates are valid, as the script will accept silly values like 2010-88-59
    Last edited by Philip M; 05-24-2010 at 06:05 PM.

  • #8
    New to the CF scene
    Join Date
    May 2010
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you for your help.
    Now its working fine.
    I think at this stage I don't have to think for date like 2009-92-22, because I am now accepting the date from a calender.


  •  

    Posting Permissions

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