Click here to Skip to main content
15,904,497 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
<asp:TextBox ID="txtStartDate" CssClass="Form_InputField" runat="server">


this is my text box .. how can i validate that value entered in this must not be greater than todays date ...
Posted
Updated 14-Jun-21 22:43pm
Comments
Thomas ktg 25-Sep-13 2:35am    
You can validate it in several ways. How do you want to validate? Do you want to validate through javascript or serverside? And what have you tried with regarding validation of date?
Torakami 25-Sep-13 2:43am    
I want to do that using some validator controls if possible

For System.DateTime, operator '<=' is defined, among others.
To get a value from a string, use one of the methods Parse, ParseExact, TryParse or TryParseExact depending on required format or culture. Please see:
http://msdn.microsoft.com/en-us/library/system.datetime.aspx[^].

However, using a text box to edit date/time is not really a good idea. It's much better to use some DateTimePicker. Please see my past answer: TextBox allow only (HH:mm tt) format using asp.net[^].

—SA
 
Share this answer
 
Comments
Torakami 25-Sep-13 2:45am    
Yes i have used date time picker only but its in jquery .. how can i check here i dont understand ..


$("#<%=txtStartDate.ClientID%>").datepicker({
showOn: 'both', showAnim: '', changeMonth: true,
changeYear: true, buttonImage: '../images/calendar.gif',
buttonImageOnly: true, dateFormat: 'dd/mm/yy'
});
Sergey Alexandrovich Kryukov 25-Sep-13 3:21am    
What cannot you understand? if (time2 <= time1)... if (time2 >= time1)...
—SA
Refer these links to validate the date format.

http://stackoverflow.com/questions/3005425/how-to-check-or-validate-the-textbox-entered-date-is-in-dd-mm-yyyy-format[^]

after this add the below code to validate the date.

C#
DateTime currentdate = Convert.ToDateTime(DateTime.Now.ToString("dd/MM/yyyy"));
DateTime ToDate = DateTime.ParseExact(e.Value,"dd/MM/yyyy", CultureInfo.InvariantCulture);
    if (ToDate > currentdate)
                {
                    lblShowDateError.Visible = true;
                    lblShowDateError.Text = "End Date should not be earlier than the current date.";
                    return;
                }
                else
                {
                    lblShowDateError.Text = "";
                }    
 
Share this answer
 
Comments
Er Daljeet Singh 25-Sep-13 3:54am    
This solution is really good.But on which event i should write this code whether i should write in it TextChanged event or any other event.
Thomas ktg 25-Sep-13 5:07am    
write it in CustomValidator1_ServerValidate event itself.
Er Daljeet Singh 25-Sep-13 6:05am    
Thanks Thomas....
If u use calenderExtenderextender control.. enter your code like this at script tag...u cal this method in calenderextender control add this properity also...
XML
OnClientDateSelectionChanged="checkDate"

<pre lang="xml">&lt;script type=&quot;text/javascript&quot;&gt;

        function checkDate(sender, args) {
            if (sender._selectedDate &gt; new Date()) {
                alert(&quot;You cannot select a future date!&quot;);
                sender._selectedDate = new Date();
                // set the date back to the current date
                sender._textbox.set_Value(sender._selectedDate.format(sender._format))
            }
        }</pre>
 
Share this answer
 

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