Click here to Skip to main content
15,949,686 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I am using ajax calendarextender and java script to restrict that the user will not select the past date but here i m facing an issue. if the user is selecting the past date then it is working fine ,if the user is selecting future date then also it is working fine but if the user is selecting the current date then also he is getting alert which is not required. My requirement is that the user can select current date plus the future date but restrict to enter any previous date.Please help me to solve this issue. below you can find the code:

ASP.NET
<!-- ============================== Fieldset 1 ============================== -->
		<fieldset>
			<legend>Enter Date</legend>
            <table>
            <tr>
            <td><label for="input-one" class="float">Date</label>
            </td><br />
            <td><asp:TextBox ID="txtDate" runat="server" CssClass="inp-text" Width="300px"></asp:TextBox> </td>
            <td><asp:Image runat="server" ID="btnDate2" AlternateText="cal2" ImageUrl="~/App_Themes/Images/icon_calendar.jpg" />
                <ajaxtoolkit:calendarextender  runat="server" ID="calExtender2" Format="dddd, MMMM dd, yyyy" PopupButtonID="btnDate2" TargetControlID="txtDate"  OnClientDateSelectionChanged="CheckDateEalier" /> </td>
            </tr></table>
				
		</fieldset>
		<!-- ============================== Fieldset 1 end ============================== -->


Javascrpt:-

JavaScript
<script type="text/javascript">
        function CheckDateEalier(sender, args)
            {
                if (sender._selectedDate < new Date())
                 {
                    alert("Day earlier than today cannot be selected.");
                    sender._selectedDate = new Date();
                    sender._textbox.set_Value(sender._selectedDate.format(sender._format))
                }
            }
        
</script>
Posted

1 solution

<script type="text/javascript">
        function checkDate(sender, args) {
            var toDate= new Date();
            toDate.setMinutes(0);
            toDate.setSeconds(0);
            toDate.setHours(0);
            toDate.setMilliseconds(0);
            if ( sender._selectedDate < toDate) {
                alert("You can't select day earlier than today!");
                sender._selectedDate = toDate;
                //set the date back to the current date
                sender._textbox.set_Value(sender._selectedDate.format(sender._format))
            }
        }
    </script></script>
 
Share this answer
 
Comments
Nisha.Vasmate 2-Aug-12 1:43am    
THANKS, HELPED A LOT....
sanjay06 15-Apr-14 10:10am    
Thanks It Works well
mazharkhan123 28-Jul-16 3:10am    
Thank you code 89

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900