Labels

Monday, November 29, 2010

Date Validation

Mostly found, data validation is slightly tricky. Checking against only Regurlar experession deos not serves the purpose. Here is the right apporach.
It involves two steps.

1.       Check for proper format – Using Regex -
2.       Chech for Date validity against Feburary Month (with and without Leap Year).

Here it is:



Step 1
string REGEXPR = @"^(0?[1-9]|1[012])[- /.](0?[1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d$";

bool res = Regex.IsMatch(yourDate, REGEXPR);

Step2:
string date = "02/29/2009";
bool res = true;

try
{
    DateTime testDate = Convert.ToDateTime(date);
}
catch
{
   res = false;
}

MessageBox.Show(res.ToString()); // False

These steps certifies the date correctness.

Hope this helps..

Regards,
 Arun Manglick

No comments:

Post a Comment