Click here to Skip to main content
15,885,868 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
hi guys how do i validate a textbox to show error msg if date entered is in the past

i nid sumthing like this :

C#
string Duedate;
    txtDate.Text = Calendar1.SelectedDate.ToString("yyyy/MM/dd");
    Duedate = txtDate.Text;

        
    date = DateTime.Now;
       
    if (txtDate.Text != "" && Duedate >= date )   
        {
          do some coding for future date here
        }
    else
        {
           lblErrMsg.text =" the entered date is in the past ";
        }
Posted
Updated 28-Aug-14 2:48am
v2
Comments
Xavier Nishanth 28-Aug-14 8:23am    
System.Date.Now , it will give current date , if you compare whether entered date is pas date.

You need them as date objects in JavaScript so you can compare them.

For example:

JavaScript
var selectedDate = new Date(txtDate.Text);
var date = new Date();

if (selectedDate < date)...
 
Share this answer
 
Comments
Nathan Minier 28-Aug-14 8:32am    
This is C#, and he's already got a date object that he's converting to string. No point converting it back when we can just use the previous value.
ZurdoDev 28-Aug-14 8:45am    
True. I didn't look closely enough that it was C#.
C#
txtDate.Text = Calendar1.SelectedDate.ToString("yyyy/MM/dd");
string Duedate = txtDate.Text;
string date = (DateTime.Now).ToString("yyyy/MM/dd");

if ((txtDate.Text != "") && (Convert.ToDateTime(Duedate) >= Convert.ToDateTime(date)) )
{
do some coding for future date here
}
else
{
lblErrMsg.text =" the entered date is in the past ";
}
 
Share this answer
 
Comments
Herman<T>.Instance 28-Aug-14 8:50am    
why not:
if (Calendar1.SelectedDate < DateTime.Now)
{
code here
}....
Pravuprasad 28-Aug-14 9:01am    
here calendar1.SelectedDate is in string format and DateTime.Now is in datetime format. which will never be compairable as the datetime contains some time with it.
ChintanShukla 28-Aug-14 9:31am    
This wont work I guess.
Pravuprasad 29-Aug-14 1:16am    
why? explain plz
You're trying to compare a string to a DateTime object.

C#
txtDate.Text = Calendar1.SelectedDate.ToString("yyyy/MM/dd");
if(string.isNullOrwhitespace(txtDate.Text)
{
   lblErrMsg.text =" You must enter a date. ";
} else {

    if (Calendar1.SelectedDate >= DateTime.Now)
    {
        // do some coding for future date here
    }
    else
    {
        lblErrMsg.text =" the entered date is in the past ";
    }
}



Code not compiled, so I apologize in advance for any spelling issues.
 
Share this answer
 
Try this

C#
string str = "12/10/2014"; // in your case your calendar control
string strFormat = "dd/MM/yyyy";
DateTime x;
DateTime y;

TryParseDate(str,strFormat,out x) 
TryParseDate(DateTimeNow.ToString("dd/MM/yyyy"),strFormat,out y))


if(x > y)
{
lblErrMsg.text =" the entered date greater then current date" ;

}

private bool TryParseDate(string str,string strFormat, out DateTime result)
{
    if(!String.IsNullOrEmpty(str))
     {
          return DateTime.TryParseExact(str,strFormat,new CultureInfo("en-US"),DateTimeStyles.None,out result)
     }
    else
     {
          result = new DateTime(); return false;
     }


}
 
Share this answer
 
v2
Comments
ChintanShukla 28-Aug-14 9:01am    
Format date according to your need! :)

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