asp:RegularExpressionValidator的正则表达式,格式为MMddyy(闰年问题)

Mal*_*lik 3 regex asp.net date leap-year

我们需要有关使用asp.net asp:RegularExpressionValidator的正则表达式的帮助来验证MMddyy格式的日期.我们面临的问题是闰年.问题是可以通过正则表达式验证它只接受有效的闰年日期,例如02/29/2008是有效日期,但02/29/2010不是有效日期.

任何与"asp:RegularExpressionValidator"一起使用的正则表达式?

Tim*_*ker 15

好的,你问了一个正则表达式.这里是.我认为为什么用正则表达式验证日期并不是一个好主意,这一点很明显:

首先,详细的评论版本至少可以让人理解这种野兽:

^       # start of string
(?:     # either match...
 (?:
  (?:   # 31st day of all allowed months
   (?:(?:0?[13578]|1[02])/31)
   |    # or
   (?:(?:0?[13-9]|1[0-2])/(?:29|30))
  )     # 29th/30th day of any month except February
  /     # plus any year since 1600
  (?:1[6-9]|[2-9]\d)
  \d{2}
 )
|       # or
 (?:    # match Feb 29th
  0?2/29/
  (?:   # in all leap years since 1600
   (?:
    (?: # century
     1[6-9]|[2-9]\d
    )
    (?: # two-digit years divisible by four, not ending in 00
     0[48]
     |
     [2468][048]
     |
     [13579][26]
    )
    |
    (?: # all the leap years ending in 00
     (?:16|[2468][048]|[3579][26])
    00
    )
   )
  )
 )
|       # or
 (?:    # (for any month)
  (?:0?[1-9])
  |
  (?:1[0-2])
 )
 /
 (?:    # match the 1st-28th day
  0?[1-9]|1\d|2[0-8]
 )
 /
 (?:
  (?:1[6-9]|[2-9]\d)\d{2}
 )
)$
Run Code Online (Sandbox Code Playgroud)

或者,如果您不能在ASP.NET验证器中使用详细的正则表达式:

^(?:^(?:(?:(?:(?:(?:0?[13578]|1[02])/31)|(?:(?:0?[13-9]|1[0-2])/(?:29|30)))/(?:1[6-9]|[2-9]\d)\d{2})|(?:0?2/29/(?:(?:(?:1[6-9]|[2-9]\d)(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))|(?:(?:0?[1-9])|(?:1[0-2]))/(?:0?[1-9]|1\d|2[0-8])/(?:(?:1[6-9]|[2-9]\d)\d{2}))$)$
Run Code Online (Sandbox Code Playgroud)

这些允许但不要求在一位数月/天内使用前导零.如果你不希望出现这种情况,则更换的所有实例0?0.