c#
how can i validate the dateformat(mm/dd/yyyy) in a textbox using C#
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Shivshanker CheralPosted Sep 11, 2009, 2:41 AM
dd/mm/yyyy Format In regular Expression (optional mm,optional dd)
([1-9]|0[1-9]|[12][0-9]|3[01])[- /.]([1-9]|0[1-9]|1[012])[- /.][0-9]{4}$
mm/dd/yyyy (optional mm,optional dd)
^([1-9]|0[1-9]|1[012])[- /.]([1-9]|0[1-9]|[12][0-9]|3[01])[- /.][0-9]{4}$
mm/dd/yyyy (Exact Format)
^([01]\d)/([0-3]\d)/(\d{4})$
OR Try this function
private void DateSubmit_Click(object sender, System.EventArgs e)
{
DateTime date1, date2;
bool date1OK, date2OK;
date1 = new DateTime(1,1,1);
date2 = new DateTime(1,1,1);
try
{
date1 = Convert.ToDateTime(Date1.Text);
date1OK=true;
}
catch
{
date1OK = false;
}
try
{
date2 = Convert.ToDateTime(Date2.Text);
date2OK=true;
}
catch
{
date2OK = false;
}
if(date1OK && date2OK)
{
if(date1.CompareTo(date2) > -1)lblResult.Text = "Date2 must be after Date1";
else
{
//do whatever here
}
}
else lblResult.Text = "Invalid date format. Please check your input in the Date1 and Date2 fields";
}