我有以下regex日期验证器,但它允许以下日期:
"99/99/2012"只是不确定为什么,如果有人知道如何更改regex来检查上面的内容
日期格式如下: 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)))$发布于 2012-06-15 01:01:10
您可以使用此正则表达式:
适用于闰年的
((^(10|12|0?13578)(/)(301|12|0?1-9)(/)((18-9\d{2})|(2-9\d{3}))$)|(^(11|0?469)(/)(30|12|0?1-9)(/)((18-9\d{2})|(2-9\d{3}))$)|(^(0?2)(/)(20-8|10-9|0?1-9)(/)((18-9\d{2})|(2-9\d{3}))$)|(^(0?2)(/)(29)(/)(246800)$)|(^(0?2)(/)(29)(/)(357900)$)|(^(0?2)(/)(29)(/)(18948)$)|(^(0?2)(/)(29)(/)(2-9)$)|(^(0?2)(/)(29)(/)(189048)$)|(^(0?2)(/)(29)(/)(2-92468)$)|(^(0?2)(/)(29)(/)(18926)$)|(^(0?2)(/)(29)(/)(2-913579)$))
是的,有点吓人,但我已经使用它很多年了,取得了很大的成功。
您可以使用this online expression tester测试它。
享受吧!
发布于 2012-06-15 01:54:29
我在这些模式中看到了很多过于复杂的地方。你的日期格式实际上非常简单。我认为你采取了错误的方法,忽略了28天一个月和31天一个月之间的重叠。
在我进一步解释之前,让我指出,除非你坚持使用正则表达式,还有更好的方法来解析和验证日期。最好的选择是DateTime.TryParseExact,它就是为此而生的。如果你使用的是WebForms,你可以直接使用一个带有Operator="DataTypeCheck"和Type="Date"的CompareValidator。
现在,我已经说完了这些神奇的单词,让我们来讨论正则表达式模式。
改变你看待这件事的方式。并不是所有的月份都包含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,您可以使用lookarounds将其缩短相当多:
^(
(?!(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] ) )
)$发布于 2012-06-15 01:24:26
只是抛出了另一个选项来考虑...
我喜欢简单的正则表达式,但疯狂的正则表达式很难验证和维护。
您是否可以编写自己的验证器并使用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
}https://stackoverflow.com/questions/11037883
复制相似问题