Click here to Skip to main content
15,885,985 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
I have a text box where a user may enter anything. I need to check if the user entered a date. I want true or false.
Please suggest

Thanks!
Posted
Updated 20-Apr-14 8:07am
v2
Comments
[no name] 20-Apr-14 10:26am    
Don't use a textbox. Use the proper control for the job. Use a Date time picker.

Use TryParse method[^]
C#
using System;

public class Program
{
    public static void Main()
    {
          // from your TextBox1.Text
          string TextboxText = "2/22/2014";

          DateTime dt;

          if (DateTime.TryParse(TextboxText, out dt))
          {
               Console.WriteLine("It is a date");
          }
          else
          {
              Console.WriteLine("It is not a date");
          }
    }
}
 
Share this answer
 
v2
Comments
Aarti Meswania 20-Apr-14 10:33am    
detailed solution. 5+! :)
Peter Leow 20-Apr-14 11:03am    
Thank you.
Aarti Meswania 20-Apr-14 11:05am    
welcome! :)
Daniel Mashukov 21-Apr-14 7:35am    
THANKS!
Best way? Parse it to a date:
C#
DateTime dt;
book isOK = DateTime.TryParse(myTextBox.Text, out dt);
 
Share this answer
 
Comments
Aarti Meswania 20-Apr-14 10:28am    
5+ :)
Volynsky Alex 21-Apr-14 4:27am    
Nice answer!
Maybe The Best solution is to parse it into date.

Or try to change the TextBox to MaskedTextBox Control so you can add a Date Mask that will allow to the client to enter a date format
 
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