博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

.Net中判断日期时间输入是否合法(使用正则表达式)  

^((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-))$


public static bool validateDatetime(string strValue)
{
   string strReg = @"([1-2][0-9][0-9][0-9])-(0*[1-9]|1[0-2])-(0*[1-9]|[12][0-9]|3[01])\ (0*[0-9]|1[0-9]|2[0-3]):(0*[0-9]|[1-5][0-9]):(0[0-9]|[1-5][0-9])";  
   if (strValue == "")
   {
    return false;
   }
   else
   {
    Regex re = new Regex(strReg);
    MatchCollection  mc = re.Matches(strValue);
    if (mc.Count == 1)
     foreach(Match m in mc)
     {
      if (m.Value == strValue)
       return true;
     }
   }
   return false;
}


这个太xxxxxxxxx了

程序代码 程序代码
public   bool   IsDate(string   str)  
  {  
            bool   Result=true;  
            try  
              {  
                    System.Convert.ToDateTime(str);  
              }  
              catch  
              {  
                        Result=false;  
                }  
      return   Result;  
  }