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 5 of 5
  1. #1
    New Coder
    Join Date
    Feb 2004
    Posts
    54
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Date Validation - Hows This?

    i have just done a date validation script in javascript. i am using 3 dropdownlists for my date selector. the only way there can be a validation failure is if somebody picks a day which is too large for the selected month e.g. '31 February 2001'.

    i have noticed that if this happens, when i create a Date() object with Javascript then it puts the date into the next month. So my validation script is simply creating a Date object then testing if the month of the resulting Date is the same or not. Seems to easy! i was wondering what you guys thought of this, im by no means an expert Javascript developer. is this ok?

    function IsDateValid(object, arguments)
    {
    var selectDay=document.getElementById('ddlDay');
    var selectMonth=document.getElementById('ddlMonth');
    var selectYear=document.getElementById('ddlYear');
    var theDay=selectDay.options[selectDay.selectedIndex].value;
    var theMonth=selectMonth.options[selectMonth.selectedIndex].value - 1;
    var theYear=selectYear.options[selectYear.selectedIndex].value;
    var theDate=new Date(theYear,theMonth,theDay,0,0,0,0);
    arguments.IsValid=(theDate.getMonth()==theMonth)?true:false;
    }

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,082
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    "When I create a Date() object with Javascript then it puts the date into the next month."

    The date.getMonth() method returns the month of the year as an index value 0 (January) to 11 (December).

  • #3
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    The date.getMonth() method returns the month of the year as an index value 0 (January) to 11 (December).
    That's why there is a -1 on the end of the statement that creates the theMonth variable. theMonth gets set to a value between 0 and 11 and the test at the end sees if a day number that exceeds the number of days in that month has pushed the date into the following month (eg 31Nov2006 will set the date to 1Dec2006).

    i was wondering what you guys thought of this, im by no means an expert Javascript developer. is this ok?
    The test should work perfectly for validating the date as selected from the dropdown lists since the only combinations that are able to be selected that are invalid will equate to dates in the following month as you discovered. It certainly saves having to test if it is a leap year when the date is in February (but assumes that the Javascript Date implementation has the leap year calculation right - which is true for at least dates between 1901 and 2099).

    You can shorten the code slightly (without changing what it does) by getting rid of the 0,0,0 in the end of the Date constructor and by changing the last line of the code to:

    arguments.IsValid=theDate.getMonth()==theMonth;
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #4
    New Coder
    Join Date
    Feb 2004
    Posts
    54
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thanks for the feedback.

    its interesting what you say about the leap year only being 100% certain after 1901. but im prepared to let that go as im not expecting many users aged over 105!!

  • #5
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    The year 1900 was not a leap year so if the browser was coded to just test for leap years by dividing by 4 then it would get it wrong. Most programmers should know the leap year rules but there may be some who don't which is why I suggested that there may be browsers that get it wrong.

    The leap year rules are:
    1. A year divisible by 4 but not by 100
    if (year%4==0 && year%100!=0)

    2. A year divisible by 400
    if (year%400==0)

    3. One extra leap year every 3300 years. As the current calendar was first introduced in 1582 I have seen it suggested that 4882 and every 3300 years after that would be the years in which those extra leapyears would be added. Don't think most people need worry about this one for a while yet.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.


  •  

    Posting Permissions

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