普通的前pression为ASP:RegularEx pressionValidator与MMDDYY格式(闰年问题) [英] Regular expression for asp:RegularExpressionValidator with format MMddyy (leap year issue)

查看:217
本文介绍了普通的前pression为ASP:RegularEx pressionValidator与MMDDYY格式(闰年问题)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们需要帮助定期EX pression与asp.net ASP工作:RegularEx pressionValidator来验证MMDDYY格式的日期。我们面临的问题是闰年。问题是,是否有可能通过定期EX pression以验证它仅接受有效的闰年日期,就像2008年2月29日是一个有效的日期,但2010年2月29日是不是有效日期。

任何常规的前pression一个使用的ASP:RegularEx pressionValidator?


解决方案

OK,你问一个正则表达式。这里是。我认为这是显而易见的,为什么它不验证使用常规的前pression日期是一个好主意:

首先,冗长的评论版,至少使这种理解可能兽:

字符串

  ^#开始
(?:#无论是比赛...
 (?:
  (:?#31的所有允许月份的一天
   (:( ?: 0 [13578] |?1 [02])/ 31)
   | # 要么
   (:( ?: 0 [13-9] |?1 [0-2])/(?:29 | 30))
  )#29日/月,除了每月的30日
  /#加上自1600年以来任何一年
  (?:1 [6-9] | [2-9] \\ D)
  \\ D {2}
 )
| # 要么
 (?:#比赛2月29日
  0×Ψ2/29 /
  (?:#在所有的闰年,因为1600
   (?:
    (?:#世纪
     1 [6-9] | [2-9] \\ D
    )
    (?:#两位数年四整除,在00没有不散
     0 [48]
     |
     [2468] [048]
     |
     [13579] [26]
    )
    |
    (?:#所有的闰年在00结束
     (?:16 | [2468] [048] | [3579] [26])
    00
    )
   )
  )
 )
| # 要么
 (?:#(任何月)
  (:?0 [1-9])
  |
  (?:1 [0-2])
 )
 /
 (?:#匹配1至28天
  ?0 [1-9] | 1 \\ D | 2 [0-8]
 )
 /
 (?:
  (?:1 [6-9] | [2-9] \\ D)\\ D {2}
 )
)$

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

<$p$p><$c$c>^(?:^(?:(?:(?:(?:(?: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}))$)$

这允许但不要求在个位数的月/日前导零。如果你不希望出现这种情况,与 0 替换 0 的所有实例。

We need help for regular expression that work with asp.net asp:RegularExpressionValidator to validate date in MMddyy format. Problem we are facing is leap year. Issue is that is it possible to verify through regular expression that it only accepts valid leap year dates like 02/29/2008 is a valid date but 02/29/2010 is not a valid date.

Any regular expression that works with "asp:RegularExpressionValidator"?

解决方案

OK, you asked for a regex. Here it is. I think it's immediately obvious why it's not a good idea to validate a date with a regular expression:

First, the verbose, commented version to at least make understanding this beast possible:

^       # 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}
 )
)$

Or, if you can't use verbose regexes in ASP.NET validators:

^(?:^(?:(?:(?:(?:(?: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}))$)$

These allow but do not require a leading zero in single-digit months/days. If you don't want that, replace all instances of 0? with 0.

这篇关于普通的前pression为ASP:RegularEx pressionValidator与MMDDYY格式(闰年问题)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆