2012-12-31 30 views
1

我有一個日期字段,格式爲「dd-M-y」,例如01-Jan-2013。首先,我想檢查格式必須是「dd-M-y」,其次日期不應該在過去,但可以是今天和以後。如何驗證字符串是格式爲dd-M-y的日期,而不是過去的日期?

我會怎麼做呢?我想使用正則表達式,但我不知道適合的是什麼。

+1

我認爲最好先將它轉換爲DateTime對象。這將使日期操作和驗證更容易和更強大。 –

回答

2

你應該使用DateTime.TryParseExact而不是使用正則表達式來驗證您的DateTime

string testDate = "01-Jan-2013"; 
DateTime temp; 
if (DateTime.TryParseExact(testDate, 
          "dd-MMM-yyyy", 
          CultureInfo.InvariantCulture, 
          DateTimeStyles.None, 
          out temp) && 
    (temp > DateTime.Today)) 
{ 
    //Valid date and greater than today 
} 
else 
{ 
    //Invalid date or less than today 
} 
0

我想你應該綁定用戶填寫正確的格式,而不是檢查它的日期......

的在這種情況下最好的解決方案將是MaskEditExtender

相關問題