SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Wizard donboe's Avatar
    Join Date
    Jun 2010
    Location
    Netherlands
    Posts
    2,097
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Check if dropdown month is eq to current month

    For a reservation form I need to check if the chosen day is not the current day and the chosen month is not the current month, since the reservation can not be made on the current day!

    The drop down for the months is made with an array of month names:
    Code:
    var months = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sept','Oct','Nov','Dec'];
    
            for(var i = 0; i < 12; i++){
              var opt = document.createElement('option');
              opt.value = months[i];
              opt.text    = months[i];
              monthdropdown.appendChild(opt);
            }
    I tried to check the day and month the following way:
    Code:
    var todaysDate      =  new Date();
    var dd                    =  todaysDate.getDate();
    var mm                  =  todaysDate.getMonth() + 1;
    
    if(daydropdown <= dd && monthdropdown <= mm )
    {
                $("#send_status").html('<div class="error" style="color: #C00 !important;">Reservations for today can only made by telephone!!.</div>');
                $("#daydropdown").focus();
    }
    But the month check isn't working since the month dropdown is build up from the array. So how do I check if the chosen month is the current month.

    Thank you in advance.
    “Good artists copy, great artists steal” (Pablo Picasso - 1934)

  2. #2
    Grüße aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,938
    Mentioned
    214 Post(s)
    Tagged
    12 Thread(s)
    Hi donboe,

    It all depends on what daydropdown and monthdropdown are.
    They sound like select elements and it is their value (not the elements themselves) that you would need to check

    As ever, could you post a link of some HTML?

  3. #3
    SitePoint Wizard donboe's Avatar
    Join Date
    Jun 2010
    Location
    Netherlands
    Posts
    2,097
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Pullo. They are indeed select boxes, and this is the link to the particular page. Thank you in advance
    “Good artists copy, great artists steal” (Pablo Picasso - 1934)

  4. #4
    Grüße aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,938
    Mentioned
    214 Post(s)
    Tagged
    12 Thread(s)
    Hi donboe,

    Just looking at this now.
    You have three (plus) elements with the id "form_wrapper".
    You will need to sort this.
    I'll get back soon with the rest.

  5. #5
    Grüße aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,938
    Mentioned
    214 Post(s)
    Tagged
    12 Thread(s)
    Hi donboe,

    What about:

    Code:
    $("#daydropdown, #monthdropdown, #yeardropdown").on("change", function(){
      var today = new Date(),
          dd = today.getDate(),
          mm = today.getMonth() + 1;
          selDay = Number($("#daydropdown").val()),
          selMonth = Number($("#monthdropdown").val());
    
      if (dd === selDay && mm === selMonth){
        alert("You can't reserve for today's date!")
      }
    })
    As an aside, this seems quite counter intuitive to me.
    Would it not be better to remove dates that it's not possible to select?

  6. #6
    SitePoint Wizard donboe's Avatar
    Join Date
    Jun 2010
    Location
    Netherlands
    Posts
    2,097
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Pullo.
    Quote Originally Posted by Pullo View Post
    What about:
    Code:
    $("#daydropdown, #monthdropdown, #yeardropdown").on("change", function(){
      var today = new Date(),
          dd = today.getDate(),
          mm = today.getMonth() + 1;
          selDay = Number($("#daydropdown").val()),
          selMonth = Number($("#monthdropdown").val());
    
      if (dd === selDay && mm === selMonth){
        alert("You can't reserve for today's date!")
      }
    })
    This would be a good solution indeed
    Quote Originally Posted by Pullo View Post
    As an aside, this seems quite counter intuitive to me.
    Would it not be better to remove dates that it's not possible to select?
    How would I accomplish that the past days (incl today) are not showing in the dropdown menu?

    Thank you in advance.
    “Good artists copy, great artists steal” (Pablo Picasso - 1934)

  7. #7
    Grüße aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,938
    Mentioned
    214 Post(s)
    Tagged
    12 Thread(s)
    Quote Originally Posted by donboe View Post
    How would I accomplish that the past days (incl today) are not showing in the dropdown menu?
    I would recommend using a server-side language.
    You could do this in JS, but if the user doesn't have their system clock set correctly, it could produce unexpected results.

    Do you have access to PHP?

  8. #8
    SitePoint Wizard donboe's Avatar
    Join Date
    Jun 2010
    Location
    Netherlands
    Posts
    2,097
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes I do!
    “Good artists copy, great artists steal” (Pablo Picasso - 1934)

  9. #9
    Grüße aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,938
    Mentioned
    214 Post(s)
    Tagged
    12 Thread(s)
    Well, as server time is more reliable than client time, I would make a small PHP script to return the current year, month and day.
    I would then fire off an AJAX request to this script on page load to fetch these variables.
    Once you have them, you can just adapt your JS routine to not show anything before today's date.


Bookmarks

Posting Permissions

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