Preventing the User from selecting an invalid date in a drop down menu

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mankolele
    New Member
    • Sep 2006
    • 63

    Preventing the User from selecting an invalid date in a drop down menu

    Hi all.

    I am having a tough time trying to find a way I can use to make restrictions to drop dows for dates, like the following dates are invalid and cannot be allowed to select them, April 31,June 31, September 31, November 31 and february 29 if not leap year.

    How can I put a script for the dates.

    Thank you nin advance.
  • AMT India
    New Member
    • Feb 2007
    • 64

    #2
    Originally posted by mankolele
    Hi all.

    I am having a tough time trying to find a way I can use to make restrictions to drop dows for dates, like the following dates are invalid and cannot be allowed to select them, April 31,June 31, September 31, November 31 and february 29 if not leap year.

    How can I put a script for the dates.

    Thank you nin advance.
    Try to generate dates using the function mktime. I am listing an example...
    [code=php]
    <?php
    $dateValue = '2007-01-01'; // start date
    for($i=1;$i<60; $i++){ // will print 60 days from the start date
    $pieces = explode('-',$dateValue);
    $dateValue=date ("Y-m-j", mktime(0, 0, 0, date($pieces[1]), date($pieces[2])+1,date($piece s[0]))); // get next date
    echo $dateValue;
    echo "<br>";
    }
    ?>
    [/code]

    Comment

    • pbmods
      Recognized Expert Expert
      • Apr 2007
      • 5821

      #3
      Changed thread title to better describe the problem (did you know that threads whose titles do not follow the Posting Guidelines actually get FEWER responses?).

      Comment

      • ronnil
        Recognized Expert New Member
        • Jun 2007
        • 134

        #4
        The best way to solve this is by using javascript

        you can either allow the user to input a date string and parse it with regexp like mm-dd-yyyy (the validation can be done with either javascript or PHP)

        OR

        you can make selectboxes for date, month, year (and hour&minute if you want)

        according to which year & month is selected, theres 28,29,30 or 31 dates to choose from.

        After submit you collect the actual date with mktime

        Comment

        Working...