Click here to Skip to main content
15,879,535 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have date picker and pick a date from the calendar.how to validate if user select future date to throw the error message.i know the use jquery to hide the future dates.but the problem is one date picker is use more places so don't hide the future dates.how to do it in c#. to validate the date if the future date is entered to throw the error message.
* i pick the date only. date format is (dd/mm/yyyy)

What I have tried:

C#
if (tbxFromDate.Value != "" && Convert.ToDateTime(tbxFromDate.Value) > DateTime.Today)
        {
            Messagebox.Show("From Date should be earlier or equal To Today Date", MessageHelper.MessageType.Warning);
        }

above code i will used to validation but the error message is occurs.
'String was not recognized as a valid DateTime.'
because DateTime.Today function have (mm/dd/yyyy 12:00:00 AM)
how to validate this one. i used another method also get textbox value to one string and today date(date only) stored in another string but the error occured.
error is: "to not use > in string"(little bit i forget the error message).
Posted
Updated 21-Nov-16 21:36pm

1 solution

Actually the default culture do not recognize dd/mm/yyyy as date format. So first convert it to valid datetime as i shown then compare
C#
using System.Globalization;
        DateTime dt = DateTime.ParseExact(tbxFromDate.Value, "dd/MM/yyyy", CultureInfo.InvariantCulture);
        if (tbxFromDate.Value != "" && dt > DateTime.Today)
        {
            Messagebox.Show("From Date should be earlier or equal To Today Date", MessageHelper.MessageType.Warning);
        }
 
Share this answer
 
Comments
Raja Ganapathy 22-Nov-16 6:44am    
Working fine.
Thank you so much! Er.Puneet Goel
Er. Puneet Goel 24-Nov-16 5:10am    
Welcome. :)

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