5
Answers

How to validate entered date is not greater date future date

Raja

Raja

8y
1.2k
1
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:
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).
Answers (5)
2
Manas Mohapatra

Manas Mohapatra

NA 29.3k 3.3m 8y
Try like below:
  1. DateTime dateTimeObj = DateTime.ParseExact(tbxFromDate.Value, "dd/MM/yyyy", CultureInfo.InvariantCulture);  
  2.   
  3. if (tbxFromDate.Value != "" && dateTimeObj > DateTime.Today)         
  4. {        
  5.        Messagebox.Show("From Date should be earlier or equal To Today Date", MessageHelper.MessageType.Warning);  
  6. }  
 
Accepted
2
Amresh S

Amresh S

NA 528 4.1k 8y
Hi Raja,
 
You have to parse the date value before validation. Use the below code:
  1. DateTime date = DateTime.ParseExact(this.myTextBox.Text, "dd/MM/yyyy"null);  
Here you can send the  text value entered by the user or a value picked up from your datePicker control. The next key thing is to format your Date to be parsed and if time doesn't bother you, pass the "null" value.
 
Hope this helps.
 
Regards,
Amresh S. 
1
Manas Mohapatra

Manas Mohapatra

NA 29.3k 3.3m 8y
What is the value you are getting in tbxFromDate.Value?
0
Raja

Raja

NA 1.7k 45.3k 8y

Manas Mohapatra

Amresh S

Thank You so much!
0
Raja

Raja

NA 1.7k 45.3k 8y

Manas Mohapatra

22/11/2016