Wednesday 9 October 2013

Time validation C#



using System.Text.RegularExpressions;

string strcd = DateTime.Now.ToString("hh:mm:ss tt"); //02:56:59 PM 
bool isOK = Regex.IsMatch(strcd, @"[0-2][0-9]\:[0-5][0-9]\:[0-5][0-9]\ [aApP][mM]");
if (isOK)
  {
     // Do something.
  }
else
  {



string strcd = DateTime.Now.ToString("hh:mm tt"); //01:06 PM        
bool isOK = Regex.IsMatch(strcd, @"[0-2][0-9]\:[0-5][0-9]\ [aApP][mM]");
if (isOK)
  {
     // Do something.
  }
else
  {
     // Do something else.
  }

No comments:

Post a Comment