Skip to content Skip to sidebar Skip to footer

Form Days Value Change With Respect To The Month

How can I create a day drop-down that shows the correct number of days depending on the Month selected in other drop-down? I dont want the user to enter a date of birth. Can this

Solution 1:

No, there are no built-in Javascript or PHP functions to dynamically manipulate an HTML "day in month" dropdown.

However, there are date functions in both languages (PHP · Javascript) that can tell you how many days are in a given month.

I recommend using Javascript to manipulate the children of your "day" <select /> element accordingly, when there is a change in selection in your "month" <select /> element.

Or save yourself the trouble and use the datejs library.

Good luck!

Solution 2:

1 to 28 are common for all months. for 29 onwards we have to make choice.

787:

addOption 29when feb and leapyear
addOptions 2930whenmonthwith30 days april,june,sep,nov
addOptions 293031when months with31 days   jan,march,may,july,aug,oct,dec

if clicked second time remove all added options and then re check the month and year value to find the category in which the month lies GOTO 787

<scripttype="text/javascript">var monthselected,yearselected,mtype=0,visited=0;

        functionremoveOptions(){  //removes options call each time the day selector is clicked var x=document.getElementById('dayselect');
        while(x.length>29)
            { x.remove(x.length-1);}
            }

        functionaddOptions(mtype)
        {                              //adds options acc to mtype//alert('initialising');var i;
        var x=document.getElementById("dayselect");
        for(i=29;i<=mtype;i++)
        {   var option=document.createElement("option");

        option.text=i;
        try
          {//alert('trying');// for IE earlier than version 8
          x.add(option,x.options[null]);
          }
        catch (e)
          {//alert('catching');
          x.add(option,null);
          }
        }
        }                           
        functionisleapyear(year){ //find if year is leap or notif((year%4)==0)
                {
                if((year%100)!=0)
                {
                returntrue;
                }
                elsereturnfalse;
                }
                if((year%400)==0)
                    {

                    returntrue;

                    }

        elsereturnfalse;
        }                   




        </script><html><tdid="bday" >Birthday</td><td><selectid="month"name="month"class="int"onBlur="monthselected=document.getElementById('month').value;"><optionvalue="00">Month</option><optionvalue="01" >January</option><optionvalue="02" >  February</option><optionvalue="03" >  March</option><optionvalue="04" >  April</option><optionvalue="05" >  May</option><optionvalue="06" >  June</option><optionvalue="07" >  July</option><optionvalue="08" >  August</option><optionvalue="09" >  September</option><optionvalue="10" >  October</option><optionvalue="11" >  November</option><optionvalue="12" >  December</option></select><labelid="year-label"class="year int">Year<selectclass="int"id="year"name="year"selected= onBlur="yearselected=document.getElementById('year').value;"><optionvalue="0" >YYYY</option><?phpfor($i=2012;$i>1912;$i--){echo"<option value=\"$i\">$i</option>";} ?></select><labelid="day-label"class="day int">Day<selectid="dayselect"class="int"name="day"onClick=
"if(monthselected==4||monthselected==6||monthselected==9||monthselected==11)
                                {mtype=30;}
                            if(monthselected==2&&isleapyear(yearselected))
                                {mtype=29;}
                            if(monthselected==2&&!isleapyear(yearselected))
                                {mtype=28;}

else if(mtype==0){mtype=31;} 

if(visited!=0){removeOptions();}
   addOptions(mtype);
   visited=1;"><optionvalue="0" >  DD</option><?phpfor($i=1;$i<29;$i++){
                        echo"<option value=\"$i\">$i</option>";} ?></select></label></td>

this is what i was working on today. the appropriate nodes are being added or removed on onClick event.

Post a Comment for "Form Days Value Change With Respect To The Month"