【问题标题】:ASP.NET Date Regex Validator 99/99/2012ASP.NET 日期正则表达式验证器 99/99/2012
【发布时间】:2012-06-14 16:58:00
【问题描述】:

我有以下正则表达式日期验证器,但它允许以下日期:

"99/99/2012"

只是不知道为什么,如果有人知道我可以如何更改正则表达式以检查上述内容

日期格式如下:06/05/2012 mm/dd/yyyy

正则表达式:

^(((0?[1-9]|1[012])/(0?[1-9]|1\d|2[0-8])|(0?[13456789]|1[012])/(29|30)|(0?[13578]|1[02])/31)/(19|[2-9]\d)\d{2}|0?2/29/((19|[2-9]\d)(0[48]|[2468][048]|[13579][26])|(([2468][048]|[3579][26])00)))$

【问题讨论】:

  • 您可以在客户端使用 JS 验证,查看codecorner.galanter.net/2010/07/20/… 作为奖励 - 支持各种语言环境。
  • @Trekstuff:或者更好的是 .Net RegularExpressionValidator :)
  • @Jeremy - CompareValidator 可以自动进行数据类型检查。
  • 感谢@JustinMorgan。这是个好主意。
  • @StevieB - 现在我看,我发现您发布的正则表达式与我想出的正则表达式之间存在相似之处。 Yours doesn't allow 99/99/2012,但它不必要地冗长,并且存在问题。一方面,它允许从 1920 年到 9999 年;但更重要的是我不知道为什么有|(([2468][048]|[3579][26])00)

标签: asp.net validation datetime


【解决方案1】:

我在这些模式中看到了很多过于复杂的情况。您的日期格式实际上非常简单。我认为您通过忽略 28 天月和 31 天月之间的重叠而采取了错误的方法。

在我进一步解释之前,让我指出,除非你被正则表达式所困扰,还有更好的方法来解析和验证日期。最好的选择是DateTime.TryParseExact,它是为了这。如果您使用的是 WebForms,则只需添加 CompareValidatorOperator="DataTypeCheck"Type="Date"

现在我已经说了神奇的话,让我们来谈谈正则表达式模式。

改变你看待这个的方式。确实不是所有月份都包含 31 甚至 30 天,但有多少包含 28 天?

...所有这些!因此,如果您想匹配 4 月和 5 月,则不需要覆盖 4 月 1 日至 30 日,然后是 5 月 1 日至 31 日。您可以编写一个模式来覆盖任何一个 月的前 30 天,然后自行到 5 月 31 日。结果类似于[45]/([012]?[1-9]|[123]0)|5/31,是原本长度的一半。

考虑这种模式,为了清楚起见进行了扩展:

^(
( #First, we'll cover months and days for a normal year. Forget leap years 
  #for a second.
  (
     (?<month>0?[1-9]|1[012])      #First we account for up to 28 days,
     /(?<day>[01]?[1-9]|10|2[0-8]) #which ALL months have.
  )
  |
  (
     (?<month>0?[13-9]|1[012])     #Every month but February has at
     /(?<day>29|30)                #least 30 days, so cover that.
  )
  |
  (
     (?<month>0?[13578]|1[02])     #Cover the 31st day for months
     /(?<day>31)                   #that have one.
  )
)

/(?<year>(1[89]|20)[0-9]{2})  #Any year between 1800 and 2099.

|  #Normal years: Done. Now we just need to cover February 29, 
   #and only for leap years.

(?<month>0?2)
/(?<day>29)
/(?<year>
      (1[89]|20)      #Century doesn't matter, since 100 is divisible by 4.
      (
        [24680][048]  #If the decade is even, leap years end in [048].
      |                            
        [13579][26]   #If the decade is odd, leap years end in 2 or 6.
      )
  )
)$

我们完成了。强制执行 mm/dd/yyyy 格式,验证现有日期,并且简短易读。这是缩小版:

^(((0?[1-9]|1[012])/([01]?[1-9]|10|2[0-8])|(0?[13-9]|1[012])/(29|30)|(0?[13578]|1[02])/31)/(1[89]|20)[0-9]{2}|0?2/29/(1[89]|20)([24680][048]|[13579][26]))$

编辑:

由于这是 .NET,您可以使用环视来大大缩短它:

 ^(
    (?!(0?[2469]|11)/31)
    (?!0?2/(29|30))
    (?<month>0?[1-9]|1[012])
    /
    (?!0?0/)
    (?<day>[012]?[0-9]|3[01])
    /
    (?<year>(1[89]|20)[0-9]{2})
  |
    (?<month>0?2)/(?<day>29)
    /
    #Years ending in 00 are only leap years if they're divisible by 400:
    (?!1[89]00)  
    (?<year>(1[89]|20) ( [24680][048] | [13579][26] ) )
 )$

【讨论】:

  • 一个,也许非常次要的一点是它错误地验证了2/29/1900。否则,它是一个不错的、干净的日期验证器。
  • @saluce - Wow, I had to Wikipedia that. 好收获!由于这是 .NET,因此可以通过在最后一个 year 组之前添加 (?!1[98]00) 来修复它。
【解决方案2】:

你可以使用这个正则表达式:

  • MM/DD/YYYY 格式
  • 验证从 18009999 的任何日期
  • 闰年帐户
  • 接受的格式:MM/DD/YYYY、M/DD/YYYY、MM/D/YYYY、M/D/YYYY

((^(10|12|0?[13578])([/])(3[01]|[12][0-9]|0?[1-9])([/]) ((1[8-9]\d{2})|([2-9]\d{3}))$)|(^(11|0?[469])([/])(30| [12][0-9]|0?[1-9])([/])((1[8-9]\d{2})|([2-9]\d{3})) $)|(^(0?2)([/])(2[0-8]|1[0-9]|0?[1-9])([/])((1[8-9 ]\d{2})|([2-9]\d{3}))$)|(^(0?2)([/])(29)([/])([2468][048 ]00)$)|(^(0?2)([/])(29)([/])([3579][26]00)$)|(^(0?2)([/]) (29)([/])(1[89][0][48])$)|(^(0?2)([/])(29)([/])([2-9 ][0-9][0][48])$)|(^(0?2)([/])(29)([/])(1[89][2468][048]) $)|(^(0?2)([/])(29)([/])([2-9][0-9][2468][048])$)|(^(0?2 )([/])(29)([/])(1[89][13579][26])$)|(^(0?2)([/])(29)([/] )([2-9][0-9][13579][26])$))

是的,有点吓人,但我多年来一直使用它并取得了巨大的成功。

您可以使用this online expression tester 对其进行测试。

享受吧!

【讨论】:

    【解决方案3】:

    只是抛出另一个选项来考虑......

    我喜欢简单的正则表达式,但疯狂的正则表达式很难验证和维护。

    您能否改为编写自己的验证器并使用DateTime.TryParseExact() 来完成这项工作?

    详情请见http://msdn.microsoft.com/en-us/library/ms131044.aspx

    (untested)
    
    // this is inside a custom validator
    
    DateTime d;
    
    // get dateString from the control
    if (DateTime.TryParseExact(dateString, 
                               "MM/dd/yyyy",
                               CultureInfo.InvariantCulture,
                               DateTimeStyles.None,
                               out d)
    {
         // valid dateString
    }
    else
    {
        // invalid dateString
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-06-18
      • 2011-04-30
      • 2011-05-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多